summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/ublas/benchmarks/benchmark.hpp
blob: b06cbe55f556091524826a4e44feab0d7453a9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Copyright (c) 2018 Stefan Seefeld
// All rights reserved.
//
// This file is part of Boost.uBLAS. It is made available under the
// Boost Software License, Version 1.0.
// (Consult LICENSE or http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <iostream>
#include <chrono>
#include <ctime>
#include <cmath>
#include <string>
#include <vector>

namespace boost { namespace numeric { namespace ublas { namespace benchmark {

class benchmark
{
  using clock = std::chrono::system_clock;
public:
  benchmark(std::string const &name) : name_(name) {}
  void print_header()
  {
    std::cout << "# benchmark : " << name_ << '\n'
              << "# size \ttime (ms)" << std::endl;
  }
  virtual void setup(long) {}
  virtual void operation(long) {}
  virtual void teardown() {}
  
  void run(std::vector<long> const &sizes, unsigned times = 10)
  {
    print_header();
    for (auto s : sizes)
    {
      setup(s);
      auto start = clock::now();
      for (unsigned i = 0; i != times; ++i)
        operation(s);
      auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start);
      teardown();
      std::cout << s << '\t' << duration.count()*1./times << std::endl;
    }
  }
private:
  std::string name_;
};

}}}}