<p>I need to find out time taken by a function in my application. Application is a MS VIsual Studio 2005 solution, all C code. </p>

<p>I used thw windows API GetLocalTime(SYSTEMTIME *) to get the current system time before and after the function call which I want to measure time of.
But this has shortcoming that it lowest resolution is only 1msec. Nothing below that. So I cannot get any time granularity in micro seconds. </p>

<p>I know that time() which gives the time elapsed since the epoch time, also has resolution of 1msec (No microseconds) </p>

<p>1.) Is there any other Windows API which gives time in microseconds which I can use to measure the time consumed by my function?</p>

<p>-AD</p>

[{'answer_id': 49673, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>You can try to use <a href="http://www.cplusplus.com/reference/clibrary/ctime/clock.html" rel="nofollow noreferrer">clock()</a> which will provide the number of "ticks" between two points. A "tick" is the smallest unit of time a processor can measure.</p>\n\n<p>As a side note, you can\'t use clock() to determine the actual time - only the number of ticks between two points in your program.</p>\n'}, {'answer_id': 49723, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 1, 'selected': False, 'text': '<p>On Windows you can use the \'high performance counter API\'.  Check out: <a href="http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx" rel="nofollow noreferrer">QueryPerformanceCounter</a> and <a href="http://msdn.microsoft.com/en-us/library/ms644905(VS.85).aspx" rel="nofollow noreferrer">QueryPerformanceCounterFrequency</a> for the details.</p>\n'}, {'answer_id': 49767, 'author': 'gabr', 'author_id': 4997, 'author_profile': 'https://Stackoverflow.com/users/4997', 'pm_score': 3, 'selected': False, 'text': '<p>There are some other possibilities.</p>\n\n<h2>QueryPerformanceCounter and QueryPerformanceFrequency</h2>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms644904.aspx" rel="noreferrer">QueryPerformanceCounter</a> will return a "performance counter" which is actually a CPU-managed 64-bit counter that increments from 0 starting with the computer power-on. The frequency of this counter is returned by the <a href="http://msdn.microsoft.com/en-us/library/ms644905.aspx" rel="noreferrer">QueryPerformanceFrequency</a>. To get the time reference in <strong>seconds</strong>, divide performance counter by performance frequency. In Delphi:    </p>\n\n<pre><code>function QueryPerfCounterAsUS: int64;     \nbegin\n  if QueryPerformanceCounter(Result) and\n     QueryPerformanceFrequency(perfFreq)\n  then\n    Result := Round(Result / perfFreq * 1000000);\n  else\n    Result := 0;\nend;\n</code></pre>\n\n<p>On multiprocessor platforms, QueryPerformanceCounter <strong>should</strong> return consistent results regardless of the CPU the thread is currently running on. There are occasional problems, though, usually caused by bugs in hardware chips or BIOSes. Usually, patches are provided by motherboard manufacturers. Two examples from the MSDN:</p>\n\n<ul>\n<li><a href="http://support.microsoft.com/kb/895980" rel="noreferrer">Programs that use the QueryPerformanceCounter function may perform poorly in Windows Server 2003 and in Windows XP</a></li>\n<li><a href="http://support.microsoft.com/kb/274323" rel="noreferrer">Performance counter value may unexpectedly leap forward</a></li>\n</ul>\n\n<p>Another problem with QueryPerformanceCounter is that it is quite slow.</p>\n\n<h2>RDTSC instruction</h2>\n\n<p>If you can limit your code to one CPU (SetThreadAffinity), you can use <a href="http://en.wikipedia.org/wiki/RDTSC" rel="noreferrer">RDTSC</a> assembler instruction to query performance counter directly from the processor.</p>\n\n<pre><code>function CPUGetTick: int64;\nasm\n  dw 310Fh // rdtsc\nend;\n</code></pre>\n\n<p>RDTSC result is incremented with same frequency as QueryPerformanceCounter. Divide it by QueryPerformanceFrequency to get time in seconds.</p>\n\n<p>QueryPerformanceCounter is much slower thatn RDTSC because it must take into account multiple CPUs and CPUs with variable frequency. From <a href="http://blogs.msdn.com/oldnewthing/archive/2008/09/08/8931563.aspx" rel="noreferrer">Raymon Chen\'s blog</a>:</p>\n\n<blockquote>\n  <p>(QueryPerformanceCounter) counts elapsed time. It has to, since its value is \n  governed by the QueryPerformanceFrequency function, which returns a number \n  specifying the number of units per second, and the frequency is spec\'d as not \n  changing while the system is running. </p>\n  \n  <p>For CPUs that can run at variable speed, this means that the HAL cannot \n  use an instruction like RDTSC, since that does not correlate with elapsed time. </p>\n</blockquote>\n\n<h2>timeGetTime</h2>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms713418.aspx" rel="noreferrer">TimeGetTime</a> belongs to the Win32 multimedia Win32 functions. It returns time in milliseconds with 1 ms resolution, at least on a modern hardware. It doesn\'t hurt if you run   timeBeginPeriod(1) before you start measuring time and timeEndPeriod(1) when you\'re done.</p>\n\n<h2>GetLocalTime and GetSystemTime</h2>\n\n<p>Before Vista, both <a href="http://msdn.microsoft.com/en-us/library/ms713418.aspx" rel="noreferrer">GetLocalTime</a> and  <a href="http://msdn.microsoft.com/en-us/library/ms724390.aspx" rel="noreferrer">GetSystemTime</a> return current time with millisecond precision, but they are not accurate to a millisecond. Their accuracy is typically in the range of 10 to 55 milliseconds. (<a href="http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx" rel="noreferrer">Precision is not the same as accuracy</a>)\nOn Vista, GetLocalTime and GetSystemTime both work with 1 ms resolution.</p>\n'}, {'answer_id': 356795, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>One caution on multiprocessor systems:</p>\n\n<p>from <a href="http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx</a></p>\n\n<p>On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL). To specify processor affinity for a thread, use the SetThreadAffinityMask function. </p>\n\n<p>Al Weiner</p>\n'}]
['https://Stackoverflow.com/questions/49668', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2759376/']