Is there any timing functions that will give me milliseconds.
gettimeofday() does microseconds,
and time() gives seconds at best precision
Anybody know of any that gives milliseconds?
Thanks
Is there any timing functions that will give me milliseconds.
gettimeofday() does microseconds,
and time() gives seconds at best precision
Anybody know of any that gives milliseconds?
Thanks
clock()
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
clock has an issue. How fine-grained the result can be is left up to the compiler. In other words, you may get ms in 1000 or increments. You need to check tour results.
You are better off doing something like this with gettimeofday() to get to milliseconds:
Code:#include <time.h> int pointless(void) { unsigned int i=0; while(i< 0xffffffe) { ++i; } printf("%d\n",i); return i; } int main() { struct timeval first, second, lapsed; struct timezone tzp; gettimeofday (&first, &tzp); /* lapsed time ...... */ pointless(); gettimeofday (&second, &tzp); if (first.tv_usec > second.tv_usec) { second.tv_usec += 1000000; second.tv_sec--; } lapsed.tv_usec = second.tv_usec - first.tv_usec; lapsed.tv_sec = second.tv_sec - first.tv_sec; printf("Millseconds=%d\n",lapsed.tv_sec*1000 + lapsed.tv_usec /1000); return 0; }
You probably could just make up a random number for milliseconds, it'll be close enough to real. lol
---EDIT---
And as you may see, my approach is not error free:Code:#include <sys/time.h> #include <stdlib.h> #include <stdio.h> int pointless(void) { unsigned int i=0; while(i < 0xffffffe) { ++i; } printf("%d\n", i); return i; } int main() { struct timeval first, second, lapsed; struct timezone tzp; gettimeofday (&first, &tzp); /* lapsed time ...... */ pointless(); gettimeofday (&second, &tzp); if (first.tv_usec > second.tv_usec) { second.tv_usec += 1000000; second.tv_sec--; } lapsed.tv_usec = second.tv_usec - first.tv_usec; lapsed.tv_sec = second.tv_sec - first.tv_sec; printf("Jim's Milliseconds = %d\n", lapsed.tv_sec*1000 + lapsed.tv_usec /1000); srand(time(NULL)); printf("Kleid's Milliseconds = %d\n", rand() % 1500); return 0; }
Code:kleid@Shiva:~/Programming/Laboratory$ ./a.out 268435454 Jim's Milliseconds = 513 Kleid's Milliseconds = 384 kleid@Shiva:~/Programming/Laboratory$ ./a.out 268435454 Jim's Milliseconds = 511 Kleid's Milliseconds = 1456 kleid@Shiva:~/Programming/Laboratory$ ./a.out 268435454 Jim's Milliseconds = 512 Kleid's Milliseconds = 1393 kleid@Shiva:~/Programming/Laboratory$ ./a.out 268435454 Jim's Milliseconds = 509 Kleid's Milliseconds = 1100 kleid@Shiva:~/Programming/Laboratory$
Last edited by Kleid-0; 01-17-2005 at 02:33 PM.