Skip to content

Instantly share code, notes, and snippets.

@rahul003
Last active March 22, 2018 01:01
Show Gist options
  • Save rahul003/c1c8acda3a93675938c8ab3480b8e013 to your computer and use it in GitHub Desktop.
Save rahul003/c1c8acda3a93675938c8ab3480b8e013 to your computer and use it in GitHub Desktop.
Timing code snippets
#include <iostream>
#include <chrono>
class Timer {
public:
Timer() : beg_(clock_::now()) {}
void reset() { beg_ = clock_::now(); }
double elapsed() const {
return std::chrono::duration_cast<second_>
(clock_::now() - beg_).count(); }
private:
typedef std::chrono::high_resolution_clock clock_;
typedef std::chrono::duration<double, std::ratio<1> > second_;
std::chrono::time_point<clock_> beg_;
};
int main() {
Timer t;
int num = 1000000000;
float* a = new float[num];
uint16_t* b = new uint16_t[num];
t.reset();
for (int i=0;i<num; i++) {
*(a++) += (float) *(b++);
}
std::cout << t.elapsed() << std::endl;
}
from timeit import default_timer as timer
start = timer()
# ...
end = timer()
print(end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment