Skip to content

Instantly share code, notes, and snippets.

@eddyekofo94
Created May 9, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyekofo94/0e233c8b3ca6c099c067e13832664b85 to your computer and use it in GitHub Desktop.
Save eddyekofo94/0e233c8b3ca6c099c067e13832664b85 to your computer and use it in GitHub Desktop.
//
// Created by Eddy Ekofo on 09/05/2020.
//
#pragma once
#include <chrono>
#include <iostream>
class Benchmark
{
public:
Benchmark() : start_point_(std::chrono::high_resolution_clock::now()){};
~Benchmark() { Stop(); }
void Stop()
{
auto end_time = std::chrono::steady_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(
start_point_)
.time_since_epoch()
.count();
auto end =
std::chrono::time_point_cast<std::chrono::microseconds>(end_time)
.time_since_epoch()
.count();
auto duration = end - start;
double ms = duration * 0.001;
std::cout << "Time: " << ms << " ms"
<< "\n";
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_point_;
};
@eddyekofo94
Copy link
Author

A way to benchmark your c++ code etc.
Just create the object where you wish to time you code.

{
    Benchmark benchmark;
    .....

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment