how to calculate elapsed time in c for windows

Posted on 2006-04-18
Medium Priority
Last Modified: 2010-05-18
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
Question by:ronald_pangestu
LVL 16

Expert Comment

ID: 16483159
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.


LVL 16

Accepted Solution

PaulCaswell earned 1000 total points
ID: 16483163
Alternateively, call your sort routine 1000 times.


Author Comment

ID: 16483190
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?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 16

Expert Comment

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


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.


Author Comment

ID: 16483406
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.
LVL 86

Assisted Solution

jkr earned 1000 total points
ID: 16483676
See http://www.sysinternals.com/Information/HighResolutionTimers.html ("Inside Windows NT High Resolution Timers") about what you can expect.
LVL 11

Expert Comment

ID: 16485134
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.
LVL 27

Expert Comment

ID: 16505528
I aggree with Paul. When hardware clock are not acurate enough for measurement, this is the only way.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question