Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
benchmark_report.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 25.05.2013 19:14:55 EDT
3 // File: benchmark.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __BENCHMARK_HEADER
12 #define __BENCHMARK_HEADER
13 
14 #include <ustd/iostream.hpp>
15 
16 #include <Arduino.h>
17 
18 namespace diag {
19  template<typename T>
20  double benchmark_clocks(T fct, uint16_t const & n = 100) {
21  uint16_t const N = n * clockCyclesPerMicrosecond();
22  //------------------- measure for-loop overhead -------------------
23  auto s = micros();
24  for(uint16_t i = 0; i < N; ++i)
25  ;
26 
27  auto e = micros();
28  double overhead = e - s;
29  overhead *= clockCyclesPerMicrosecond() / double(N);
30 
31  //------------------- measure actual fct -------------------
32  s = micros();
33  for(uint16_t i = 0; i < N; ++i)
34  fct();
35 
36  e = micros();
37  double res = e - s;
38  res *= clockCyclesPerMicrosecond() / double(N);
39  res -= overhead;
40  return res;
41  }
42 
43  template<typename T>
44  double benchmark_micros(T fct, uint16_t const & n = 100) {
45  return benchmark_clocks(fct, n) / clockCyclesPerMicrosecond();
46  }
47 
48  template<typename T>
49  void benchmark_report(T fct, uint16_t const & n = 100) {
51  ustd::cout << F("----------------------") << ustd::endl;
52  ustd::cout << F(" Benchmark Report ") << ustd::endl;
53  ustd::cout << F("----------------------") << ustd::endl;
54  double us = benchmark_micros(fct, n);
55  if(us > 1)
56  ustd::cout << GREEN << F(" used time: ") << GREENB << us << F(" us") << ustd::endl;
57  else
58  ustd::cout << YELLOW << F(" used time: ") << YELLOWB << us * 1000 << F(" ns") << ustd::endl;
59 
60  ustd::cout << GREEN << F(" cycles : ") << GREENB << benchmark_clocks(fct, n) << NONE << ustd::endl;
61 
62  if(us > 1)
63  ustd::cout << GREEN << F(" freqency : ") << GREENB << 1000/us << F(" kHz") << NONE << ustd::endl;
64  else
65  ustd::cout << YELLOW << F(" freqency : ") << YELLOWB << 1/us << F(" MHz") << NONE << ustd::endl;
66 
67  ustd::cout << F(" (running at ") << clockCyclesPerMicrosecond() << F(" MHz)") << ustd::endl;
68  ustd::cout << F("----------------------") << ustd::endl;
69  }
70 }//end namespace diag
71 #endif //__BENCHMARK_HEADER
#define GREEN
Definition: color.hpp:15
double benchmark_clocks(T fct, uint16_t const &n=100)
Definition: benchmark_report.hpp:20
#define YELLOWB
Definition: color.hpp:18
#define GREENB
Definition: color.hpp:16
void benchmark_report(T fct, uint16_t const &n=100)
Definition: benchmark_report.hpp:49
#define NONE
Definition: color.hpp:37
struct ustd::endl_class endl
#define F(x)
Definition: ustd_generic.hpp:15
double benchmark_micros(T fct, uint16_t const &n=100)
Definition: benchmark_report.hpp:44
ustd::cout_class cout
#define YELLOW
Definition: color.hpp:17