Jump to content
Tuts 4 You

Elapsed Time Counter

Guest dreamer

Recommended Posts

Guest dreamer


Can you please help me how to write (in visual c++) a function that calculates the time elapsed to perform another function(like generating a code,benchmark......)


Link to comment
#include <iostream>
#include <windows.h>
using namespace std;LARGE_INTEGER t0,t1;
function(); //this is the function you want to time
Edited by GaBoR
Link to comment
Guest dreamer

It works perfectlly, thanks for support.

I have another question:

I have saved the ( t1.QuadPart-t0.QuadPart ) in ( LONGLONG t) so

t = t1.QuadPart-t0.QuadPart;

How can i get t converted to string to display it in a MessageBox function , i have tried the

reinterpret_cast operator but it failed.

Pleas help.


Edited by dreamer
Link to comment

lol what an odd way, isnt it better to use rdtsc to get cpu ticks, or GetTickCount to get time in ms, from both we get value and at end get another and from new substract old. everybody use it and its easiest way. to get more accurate result, set process priority to realtime and kill other tasks, switch cmd window into fullscreen. and dont screw something due in realtime if program will run in endless loop without exit, only way to shut it down will be hard reset:P

Link to comment
  • 1 month later...


Afaik, QueryPerformanceCounter is more precisely than GetTickCount and - possibly... - even bases on RDTSC.


You could also have used the function (w)sprintf(A/W), see MSDN.


Edited by metr0
Link to comment

oh yeah use maybe also msr registers and write ring0 driver to use them for show time elapsed during compression.

i use rdtsc when i profile something and GetTickCount, time in ms is enough to look what time took compression. if i optimize it for speed then cpu ticks from rdtsc are enough. none of profilers will be acurate in multitasking enviroment, to be honest even today profilers like intel vtune are not for such things, asm profiler is striped since 6.0+ last is 5.0. and 9.0 today isnt even for c++ only crap .net and java. yeah what a profiler, for high level language, low speed language like basic interpreter

Link to comment

If you're not lazy, you could consider translating these masm code timing macros into cpp, or better yet, just use asm :P



Forgot that agner releases his algos in cpp too (not same as above)




Edited by Ehtyar
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...