1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 #ifndef PROFILE_HELPERS_HPP_
9 #define PROFILE_HELPERS_HPP_
10 
11 #include <iostream>
12 #include <cassert>
13 
14 namespace profile {
15 
args(int argc,char * argv[],unsigned long & size,unsigned long & trials)16 void args(int argc, char* argv[], unsigned long& size, unsigned long& trials) {
17     size = 100000000; // Defaults.
18     trials = 10; // Default.
19     if (argc != 1 && argc != 2 && argc != 3) {
20         std::cerr << "ERROR: Incorrect argument(s)" << std::endl;
21         std::cerr << "Usage: " << argv[0] << " [SIZE] [TRIALS]" <<
22                 std::endl;
23         std::cerr << "Defaults: SIZE = " << double(size) << ", TRIALS = " <<
24                 double(trials) << std::endl;
25         exit(1);
26     }
27     if (argc >= 2) size = atol(argv[1]);
28     if (argc >= 3) trials = atol(argv[2]);
29 
30     std::clog << "vector size = " << double(size) << std::endl;
31     std::clog << "number of trials = " << double(trials) << std::endl;
32     std::clog << "number of calls = " << double(size) * double(trials) <<
33             std::endl;
34 }
35 
display(const unsigned long & size,const unsigned long & trials,const double & sum,const double & trials_sec,const double & decl_sec=0.0)36 void display(const unsigned long& size, const unsigned long& trials,
37         const double& sum, const double& trials_sec,
38         const double& decl_sec = 0.0) {
39     std::clog << "sum = " << sum << std::endl;
40     std::clog << "declaration run-time [s] = " << decl_sec << std::endl;
41     std::clog << "trials run-time [s] = " << trials_sec << std::endl;
42 
43     double avg_sec = decl_sec + trials_sec / trials;
44     std::clog << "average run-time [s] = declaration run-time + trials " <<
45                 "run-time / number of trials = " << std::endl;
46     std::cout << avg_sec << std::endl; // To cout so it can be parsed easily.
47 
48     assert(sum == double(size) * double(trials));
49 }
50 
51 } // namespace
52 
53 #endif // #include guard
54 
55