• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

timing code execution in C

berky

2[H]4U
Joined
Aug 28, 2001
Messages
2,233
how would i go about doing the following:

[pseudocode]

get current time in nanoseconds

do a bunch of stuff

get current time in nanoseconds

return end time minus begin time

[/pseudocode]

do I use time.h and the clock() function? I was looking this up but wasn't sure what to use. what do you recommend?

Thanks.
 
Code:
#include <stdio.h>
#include <time.h>


int main()
{
   long start, finish, taken;
   start=clock();
   DoStuff();
   finish=clock;

   taken=finish-start;
   printf("Time taken was %d \n", taken);

   return 0;
}


o_O Maybe? Not sure. Give it a try.
 
Assuming you're using an intel system, use the RDTSC asm instruction. clock() is very coarse grained. Just search on google for pentium RDTSC.
 
Originally posted by FuzzyDonkey
Assuming you're using an intel system, use the RDTSC asm instruction. clock() is very coarse grained. Just search on google for pentium RDTSC.

ok, the clock thing works, but i will try this RDTSC thing just to try it.


i get a "left of rdtsc must have struct/union type" error when i call "system.rdtsc();"
what's the problem?

thanks.
 
oh crap. i just realized.... it will work for my system, but i have to run it on a Sun also.

We're timing fork() calls in my OS class. i think i'll stick with the clock() for the assignment, but i am still interested in knowing how to fix that error.
 
If you're using Unix systems, just use the time command when you run your program; no modifications needed.
 
Originally posted by ameoba
If you're using Unix systems, just use the time command when you run your program; no modifications needed.

it has to work on sun, linux, and windows. preferably without modification of the code.

i was just looking at the time() command. what is the difference between that and clock(). from what i'm reading they seem pretty much the same. :confused:
 
Back
Top