i have a C-code. i want to find its execution time.?
2009-07-06 11:11:06 UTC
But when i am running it , it is giving a different execution time every time i run. Execution time must be same as many time i run because the code is same. Can any one suggest me how to solve this problem??
Five answers:
2009-07-06 11:20:02 UTC
There will always be variation in the execution time because the circumstances aren't always exactly identical. For (easy) example, if you read data from a hard disk, then if the data you're trying to read is just past the read head, then it will take an entire disk revolution (which the program has to 'wait' for) before the program can continue.
Also, data that your program needs may be cached the second time you run it. Even if that's not the case, the result may still vary a lot.
The best thing to do when trying to get a feel for the execution time, is to time your program a bunch of times, ignoring the first one (because of caching), and then averaging the result.
sami
2009-07-06 18:26:51 UTC
there is a difference between when you run a program and when it is actually EXECUTING on the processor.
when you run a program, it will most likely go to a queu first and wait on its turn to use the shared resource which is the processor, so probably the time you are seeing is the total of waiting time + execution time, and since a waiting time can vary a lot from one run to another, you are getting inconsistent results.
you can either try to find a way to start counting only when the thread is actually being executed on the processor, or just make the counter local to the piece of code you wanna run, not to the whole program.
Greg
2009-07-06 18:19:45 UTC
Execution time will vary on any operating system that runs more than one process simultaneously.
Regularly throughout any program's execution the processor halts your program and runs the other programs.
The best you can do is run the program a number of times and take the average execution time, discarding any obviously high or obviously low times.
ctbuckweed
2009-07-06 18:17:15 UTC
If you're running in Windows or some other multi-programming environment, the computer is also running other programs so naturally, the execution times will be different. They should be in the same ballpark when it is run many times unless the computer is real busy.
Paul
2009-07-06 21:08:19 UTC
If you're running a proper operating system then just use the "time" command, e.g.
% time ./my_program
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.