clock() - Does it work?

Paulo Cavalcanti promac at gmail.com
Mon Sep 10 10:01:38 UTC 2007


Hi,

I have an animation program that uses the clock() function
for synchronization. It works fine with MinGW 3.2, but not with gcc 4.1.2(FC6).

In the later case, clock() always return 0. I saw some people pointing at
the same problem
in the past, but I thought it would have been fixed by now.

I managed to implement "getus", which seems to do what I need. Is this the
right course?

Thanks.
__________________________________________________________________

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

#define getus(tv) (tv.tv_sec*CLOCKS_PER_SEC + tv.tv_usec)
int main() {
  clock_t ca,cb;
  time_t wa,wb;
  struct timeval tva, tvb;
  double c,w;
  double d;
  char s[10];
  ca = clock();
  wa = time(0);
  gettimeofday(&tva, NULL);
  printf("hit enter when ready ");
  fflush (stdout);
  gets(s);
  cb = clock();
  wb = time(0);
  gettimeofday(&tvb, NULL);
  c = (double) (cb-ca);
  w = (double) difftime(wb,wa);
  c /= (double) CLOCKS_PER_SEC;
  d = (getus(tvb) - getus(tva))/(double) CLOCKS_PER_SEC;
  printf ("cps=%d ", CLOCKS_PER_SEC);
  printf("wall=%g, cpu=%g, us=%g\n",w,c,d);
}

-- 
Paulo Roma Cavalcanti
LCG - UFRJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.fedoraproject.org/pipermail/users/attachments/20070910/a4ca3ddc/attachment-0001.html 


More information about the users mailing list