Invalid for clock() when values in the main loop of the application

I wrote the following test code which determines the past values of clock() for every second change reported by time (NULL):

static time_t   thisTime = 0;
static clock_t  clocks = 0;
time_t nextTime = time(NULL);

if (nextTime != thisTime)
{
    clock_t nextClocks = clock();
    printf("%d\n", nextClocks - clocks);
    fflush(stdout);

    thisTime = nextTime;
    clocks = nextClocks;
}

Normally, I would expect this to print values close to CLOCKS_PER_SEC, IE. 1,000,000.

However, if I add this to the loop for rendering a applications examples, I extremely wobbly values that are incompatible and far too small.

For example, I added just after the call to render() in GoodCitizen (like add #include to the top of the file) and got these values:

10998
4000
4999
2000
2000
1000
1001
8998
8000
5999
7999
5999
12999
13998

I have the same thing with HelloWorldDisplay and basically everything which uses a loop of events/Blackberry. The applications themselves seem to operate at full framerate (although if the performance were an issue I would expect numbers that were too big, not too small).

On the other hand, if I wrap while (true) {...}, test code essentially causing the app to hang in a tight loop, I get much closer numbers of my expected values:

988850
997848
993849
995849
992849
990849
996849
991849
987850
987850
940857
975852
988850
993849

If something on the Blackberry events/render loop is originally clock() to report incorrect values.

Can someone explain this? What is the appropriate procedure for determining intervals of a second?

Thank you

Dan.

If you read the clock():

https://developer.BlackBerry.com/native/reference/com.QNX.doc.neutrino.lib_ref/topic/c/clock.html

You will notice that clock() measures CPU, not not hour wall-clock-for your process.  This explains why your {} while loop (1) would measure something close to 1000000 microseconds on a 1 second interval, because she is ready to run.

Your other tests must pass a bunch of times by blocking system calls, which do not count toward time clock() as your process does not in fact.

If you want to measure wall clock time, use ClockTime() to query the clock CLOCK_MONOTONIC, or something equivalent.  clock_gettime() can be more portable than ClockTime().

See you soon,.

Sean

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for