I didint know how to do this a few days ago, but Now Im taking a algorithm and datastructure class so I had to look up a way, it was pretty cool seeing differences.
If you're on windows.
#include <Windows.h>
Then, place this somewhere, like just globally above main or something
1 2 3 4 5 6 7
|
BOOL WINAPI QueryPerformanceCounter(
_Out_ LARGE_INTEGER *lpPerformanceCount
);
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1, t2; // ticks
double elapsedTime;
|
Then have this at the top of main
QueryPerformanceFrequency(&frequency); // Gets ticks per second
This where you want to start taking time
QueryPerformanceCounter(&t1); // Starts Timer
And this where you want to stop the time
QueryPerformanceCounter(&t2); // Ends Timer
This is how you convert the time to seconds,milliseconds,microseconds (whatever you prefer, currently its milliseconds cuz * 1000)
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000 / frequency.QuadPart; // Convert the time to milliseconds
And then you can just print out the time
1 2
|
std::cout << std::endl << "Time: " << elapsedTime << " Miliseconds.\n" <<
std::endl;
|
Hope this helped :)