Skip to content

Instantly share code, notes, and snippets.

@bruceoutdoors
Last active November 5, 2015 07:04
Show Gist options
  • Save bruceoutdoors/a1d61b605d1f17a66f5e to your computer and use it in GitHub Desktop.
Save bruceoutdoors/a1d61b605d1f17a66f5e to your computer and use it in GitHub Desktop.
Measure running time using chrono (C++11). Enable/disable printing the time by comment/uncomment the __EXE_CLOCK__ macro. This is also solution for Hashmat the Brave Warrior (UVa 10055), excluding the __EXE_CLOCK__ macro.
/*
sample run:
./measure-run-time < source-input.txt
output:
2
4
100
** elapsed time: 0.523ms **
*/
#include <iostream>
#include <cmath>
using namespace std;
//** CLOCK CODE START
#include <chrono>
auto start = chrono::high_resolution_clock::now();
#define __EXE_CLOCK__ \
auto end = chrono::high_resolution_clock::now(); \
auto elapsed = chrono::duration_cast<chrono::microseconds>(end - start); \
cout << endl << "** elapsed time: " << float(elapsed.count())/1000.0 << "ms **" << endl;
//** CLOCK CODE END
using namespace std;
int main()
{
long a, b;
while (cin >> a >> b) {
cout << abs(b - a) << endl;
}
__EXE_CLOCK__
}
10 12
10 14
100 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment