Skip to main content
gamedev.net
🔒 Locked

clock() returning 0 on Linux

Started by TheMightyDude 8 replies 3.2k views
Original Post
TheMightyDude
TheMightyDude
Hello All I have a cpp file which is suppose to display a two clock ticks values but it just returns 0 on both. And time() isnt any good due to I need values that update in ms and not in seconds. This is a very basic bit of code:

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char **argv)
{
    clock_t start,stop;
    start = clock();
    printf("start: %d\n",(int)start);
    sleep(1);
    stop = clock();
    printf("stop: %d\n",(int)stop);
    return 0;
}
Anyone know why it's doing this? This worked on my old Linux box fine, but not on this one.
joanusdmentia
joanusdmentia
Try this and see if it makes a difference:

int main (int argc, char **argv){    clock_t start,stop;    start = clock();    printf("start: %llu\n",(unsigned long long)start);    sleep(1);    stop = clock();    printf("stop: %llu\n",(unsigned long long)stop);    return 0;}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
TheMightyDude
TheMightyDude
Quote:
Original post by joanusdmentia
Try this and see if it makes a difference:

*** Source Snippet Removed ***


Nope returned the following:

start: 0stop: 0

Evil Steve
Evil Steve
Red Hat by any chance? I had this too. I never found a solution unfortunately...
kinetik_mjg
kinetik_mjg
clock(3) returns processor time, not wall-clock time. If your test did something useful rather than sleeping, your second call to clock(3) would yield a value greater than zero.
#include <stdio.h>#include <time.h>int main(int argc, char *argv[]){  int i;  for (i = 0; i < 10000000; ++i)    ;  printf("took: %fs CPU time\n", (double) clock() / CLOCKS_PER_SEC);  return 0;}

If you want high precision timers for wall-clock time, take a look at gettimeofday(2) or clock_gettime(2).
Shadowdancer
Shadowdancer
Quote:

If you want high precision timers for wall-clock time, take a look at gettimeofday(2) or clock_gettime(2).


And make sure to read the manpages all the way, there's interesting stuff about the usefulness of those functions.
TheMightyDude
TheMightyDude
Thanks for all the helpfull replies :)
I think I will go with Null and Void's idea.

Thanks
Paul kirby
abdulla
abdulla
Also note that OS X doesn't (as of 10.4) have support for clock_gettime. In case you plan on being more portable.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.