If you're on windows and you want to measure stuff down in the microseconds, investigate QueryPerformanceCounter() and QueryPerformanceFrequency(). On many systems these can resolve full processor clock periods, third of a *nano*second stuff, and I don't believe I've ever seen it any more coarse than 3.5795MHz, still well under a microsecond.
If you're on windows and you want to measure stuff down in the microseconds, investigate QueryPerformanceCounter() and QueryPerformanceFrequency(). On many systems these can resolve full processor clock periods, third of a *nano*second stuff, and I don't believe I've ever seen it any more coarse than 3.5795MHz, still well under a microsecond. You call QueryPerformanceFrequency() to determine how many counts per second the counter counts.
Then call QueryPerformanceCounter() before your code under test, and then again after. Delta the two readings of QPC and divide by the period from QPF and you get the elapsed time between the two QPC calls. Like so ... LARGE_INTEGER freq; LARGE_INTEGER t0, tF, tDiff; double elapsedTime; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&t0); // code to be timed goes HERE QueryPerformanceCounter(&tF); tDiff.
QuadPart = tF. QuadPart - t0. QuadPart; elapsedTime = tDiff.
QuadPart / (double) freq. QuadPart; // elapsedTime now has your measurement, w/resolution given by freq Evidently these access a hardware counting device that is tied to some system oscillator on the main board, in which case they shouldn't suffer jitter from software load. The resolution you get depends on your system.
FOLLOW UP Here's a very simple complete program that demonstrates the interface: #include int main(void) { LARGE_INTEGER freq; LARGE_INTEGER t0, tF, tDiff; double elapsedTime; double resolution; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&t0); // code to be timed goes HERE { Sleep(10); } QueryPerformanceCounter(&tF); tDiff. QuadPart = tF. QuadPart - t0.
QuadPart; elapsedTime = tDiff. QuadPart / (double) freq. QuadPart; resolution = 1.0 / (double) freq.
QuadPart; printf("Your performance counter ticks %I64u times per second\n", freq. QuadPart); printf("Resolution is %lf nanoseconds\n", resolution*1e9); printf("Code under test took %lf sec\n", elapsedTime); return 0; } For something as simple as this, it's quicker to skip the IDE, just save it in a foo. C file and (assuming MS VS 2008) use the command line cl foo.
C to build it. Here's the output on my system: Your performance counter ticks 3579545 times per second Resolution is 279.365115 nanoseconds Code under test took 0.012519 sec.
Thanks. This seems a bit too advanced for me :). – yCalleecharan Apr 8 '10 at 16:31 the calls are really quite simple, the way you use them is the same as with clock(), the resolution is far better than with clock(), and the worst part is working with 64 bit integers.
Well, it's also windows specific, but you did tag windows. – JustJeff Apr 9 '10 at 0:08 Thanks. I've added the header windows.
H but I still get errors. An error arises at line QueryPerformanceFrequency(&freq); The compiler complains: E2293 ) expected at line 12. Another is: QueryPerformanceCounter(&t0); error is: E2293 ) expected at line 13.
Another is tDiff = tF - t0; error is: E2096 Illegal structure operation in function main at line 57. Last error: elapsedTime = tDiff / (double) freq; error is: E2110 Incompatible type conversion in function main at line 58. I compiled this using Borland C++ BuilderX.
Any suggestion? – yCalleecharan Apr 9 '10 at 5:38 grab microsoft visual studio express 2005 or 2008, they're free, and ideal for windows work. – JustJeff Apr 9 '10 at 9:58 So the above was specific to MVS?
It won't work for Borland? – yCalleecharan Apr 9 '10 at 10:15.
On Unix (I think) systems, the time command with the name of your program as a command-line argument will tell you the time the program takes to run. Note that this measures the execution time of the whole program. If you need to test just one part, include time.
H and use the clock function, more or less like this: #include int main() { clock_t start; clock_t end; int function_time; start = clock(); function_you_want_to_time(); end = clock(); /* Get time in milliseconds */ function_time = (double)(end - start) / (CLOCKS_PER_SEC / 1000.0); return 0; } That will give you the time in milliseconds (notice the / 1000.0 part). If you want seconds, remove / 1000.0. If you want plain clock ticks, which will be more accurate, make function_time a clock_t and replace the function_time = ... line with: function_time = end - start; To time the whole program, I suggest to make a function called _main() or something, move all your program related code from main() (not the timing code!) to that function, and calling it from main().
That way, it's more clear what's the timing code and what's the rest of the program.
One downside of this is that if you're only wanting to time the actual computation portion of something, for instance, and want to not include resource/data loading times or output times or whatnot, the time command can't do this (since it times the entire execution). – Amber Apr 7 '10 at 9:49 That's true. Apparently the clock() function is better for that, but I didn't know about it.
I'll add a sentence. – Javier Badia Apr 7 '10 at 9:54 Thanks. Please see my updated post.
– yCalleecharan Apr 7 '10 at 9:56 BTW, if you want to time the whole program, place the clock() calls at the beginning and at the end. Also, can someone tell me if it's correct to divide by CLOCKS_PER_SEC? – Javier Badia Apr 7 '10 at 10:01 Thanks.
Does function_you_want_to_time() has to take an argument? I want to time the whole program. – yCalleecharan Apr 7 '10 at 10:05.
If you need a total for your program then in Linux console: $ time myProgram You can also use time. H in your code. #include int main(){ time_t start, end; start = time(0); /* some working code */ end = time(0); printf("%i seconds", end - start ); }.
I get lots of errors. Do we have to define the type of time_t, start? – yCalleecharan Apr 7 '10 at 10:02 I think it can be int also.
I didn't have to define it anywhere in my Ubuntu. – Draco Ater Apr 7 '10 at 10:05.
You could use the clock() function (in ) if you want to test a block of code, or the time program on *nix, as another answerer suggested. E.g. > time .
/foo my args For clock, you need to subtract the difference between two checkpoints. E.g. #include void f() { clock_t start, end; start = clock(); // some long code.
End = clock(); printf("Took %ld ticks\n", end-start); // or in (fractional) seconds. Printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC); } Update Regarding your new errors, you can't mix code and declarations in VC. You mustn't call any functions then continue to declare variables.
Declare all your vars at the top, or compile with C++ mode.
I get lots of errors. Do we have to define the type of clock_t, start? – yCalleecharan Apr 7 '10 at 10:03 time.
H defines clock_t. – Alex Apr 7 '10 at 10:08 Do we need to have the void f()? Or can we just put clock_t start, end; start = clock();...end = clock();printf("Took %ld ticks\n", end-start); inside main?
– yCalleecharan Apr 7 '10 at 10:29 Of course, void f() is just a sample function :) – Alex Apr 7 '10 at 10:43 Sorry, this is not working for me. MVS208 complains stupidly with 102 errors. The compiler complains that lots of things are undefined.
Are you sure you're not missing anything in your code snippet? – yCalleecharan Apr 7 '10 at 11:01.
You probably want time. H, and the clock() function.
Thanks. Please see my updated post. – yCalleecharan Apr 7 '10 at 9:55.
You can try GetTickCount also. Clock will also work fine. But, I guess clock values will change if some other process or some one manually changes the system time, wheareas GetTickCount values are not affected by that.
Yes, this is an alternative. I'll investigate this. – yCalleecharan Apr 8 '10 at 16:33.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.