1 // 2 // Copyright (c) 2018 Stefan Seefeld 3 // All rights reserved. 4 // 5 // This file is part of Boost.uBLAS. It is made available under the 6 // Boost Software License, Version 1.0. 7 // (Consult LICENSE or http://www.boost.org/LICENSE_1_0.txt) 8 9 #pragma once 10 11 #include <iostream> 12 #include <chrono> 13 #include <ctime> 14 #include <cmath> 15 #include <string> 16 #include <vector> 17 18 namespace boost { namespace numeric { namespace ublas { namespace benchmark { 19 20 class benchmark 21 { 22 using clock = std::chrono::system_clock; 23 public: benchmark(std::string const & name)24 benchmark(std::string const &name) : name_(name) {} print_header()25 void print_header() 26 { 27 std::cout << "# benchmark : " << name_ << '\n' 28 << "# size \ttime (ms)" << std::endl; 29 } setup(long)30 virtual void setup(long) {} operation(long)31 virtual void operation(long) {} teardown()32 virtual void teardown() {} 33 run(std::vector<long> const & sizes,unsigned times=10)34 void run(std::vector<long> const &sizes, unsigned times = 10) 35 { 36 print_header(); 37 for (auto s : sizes) 38 { 39 setup(s); 40 auto start = clock::now(); 41 for (unsigned i = 0; i != times; ++i) 42 operation(s); 43 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start); 44 teardown(); 45 std::cout << s << '\t' << duration.count()*1./times << std::endl; 46 } 47 } 48 private: 49 std::string name_; 50 }; 51 52 }}}} 53