• C

how to calculate elapsed time in c for windows

dear expert,

I am trying to calculate how long it takes for my quick sort function to sort an array. I want to calculate it in microseconds because obviously miliseconds and seconds still resulting in 0 elapsed time. I am using microsoft VS . Net 2003. Thank you
ronald_pangestuAsked:
Who is Participating?
 
PaulCaswellCommented:
Alternateively, call your sort routine 1000 times.

Paul
0
 
PaulCaswellCommented:
You may be looking for 'ftime'. Its in sys\timeb.h.

Take a look here for just one of the many discussions on this subject.

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20455818.htm

Paul
0
 
ronald_pangestuAuthor Commented:
i have tr using ftime() but it only able to calculate in miliseconds which is not enough. Is there anyway how to calc microseconds or nanoseconds in windows?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
PaulCaswellCommented:
A little research suggests QueryPerformanceCounter may be of use. Other methods requre assembler.

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20514943.html

Seriously though, if you are testing a sort routine it would be better to call it 1000 times.

Run through one loop, randomising an array of data and then sorting it 1000 times. Then perform another loop just randomising the data. Subtract one from the other. You'll get a much better idea of its performance across a range of data.

Paul
0
 
ronald_pangestuAuthor Commented:
Acctually i only want to run it once. Can you like take a time (in microseconds format) in the beginning of your sort and at the end of your sort or any processing? and at the end you get the difference between those two.
0
 
jkrCommented:
See http://www.sysinternals.com/Information/HighResolutionTimers.html ("Inside Windows NT High Resolution Timers") about what you can expect.
0
 
WelkinMazeCommented:
Hi,
Paul is right. Just call your routine 1000 or why not 1000000 times. And simply divide the result to 1000 (or 1000000) at the end. You will get a much better precision for your routine in this way.
0
 
NopiusCommented:
I aggree with Paul. When hardware clock are not acurate enough for measurement, this is the only way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.