GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Benchmark.C
Go to the documentation of this file.
1 
2 #include <sys/times.h>
3 #include <iostream>
4 #include <sstream>
5 
6 // g++ -O -Wall -fPIC Benchmark.C -o Benchmark
7 
8 // cast from float to double
9 //
10 /*
11 
12 
13 
14 */
15 void cast_cost(unsigned int nbloop = 100000u)
16 {
17  // to do the casting
18  double d, d1 = 10.01; float f, f1 = 10.01f; struct tms t0, t;
19 
20  // just the loop
21  times(&t0);
22  for (unsigned int i = 0u; i < nbloop; i++ ) {
23  for (unsigned int j = 0u; j < nbloop; j++ ) {
24  }
25  }
26  times(&t);
27  std::cout << "User " << float(t.tms_utime-t0.tms_utime)/sysconf(_SC_CLK_TCK)
28  << " second, System " << float(t.tms_stime-t0.tms_stime)/sysconf(_SC_CLK_TCK)
29  << " seconds" << std::endl;
30 
31 
32  // no cast
33  times(&t0);
34  for (unsigned int i = 0u; i < nbloop; i++ ) {
35  for (unsigned int j = 0u; j < nbloop; j++ ) {
36 // d = d1;
37  d = double(f1);
38  }
39  }
40  times(&t);
41  std::cout << "User " << float(t.tms_utime-t0.tms_utime)/sysconf(_SC_CLK_TCK)
42  << " second, System " << float(t.tms_stime-t0.tms_stime)/sysconf(_SC_CLK_TCK)
43  << " seconds" << std::endl;
44 
45  // cast
46  times(&t0);
47  for (unsigned int i = 0u; i < nbloop; i++ ) {
48  for (unsigned int j = 0u; j < nbloop; j++ ) {
49  d = d1;
50 // d = double(f1);
51  }
52  }
53  times(&t);
54  std::cout << "User " << float(t.tms_utime-t0.tms_utime)/sysconf(_SC_CLK_TCK)
55  << " second, System " << float(t.tms_stime-t0.tms_stime)/sysconf(_SC_CLK_TCK)
56  << " seconds" << std::endl;
57 }
58 
59 // memcpy versus assignment
60 /*
61 
62 */
63 void memcpy_cost(unsigned int nbloop = 100000u)
64 {
65  // to do the casting
66  double d, d1 = 10.01; float f, f1 = 10.01f; struct tms t0, t;
67 
68  // no cast
69  times(&t0);
70  for (unsigned int i = 0u; i < nbloop; i++ ) {
71  for (unsigned int j = 0u; j < nbloop; j++ ) {
72 // d = d1;
73  memcpy(&d,&d1,8);
74 // d = double(f1);
75  }
76  }
77  times(&t);
78  std::cout << "User " << float(t.tms_utime-t0.tms_utime)/sysconf(_SC_CLK_TCK)
79  << " second, System " << float(t.tms_stime-t0.tms_stime)/sysconf(_SC_CLK_TCK)
80  << " seconds" << std::endl;
81 
82  return;
83 
84  // cast
85  times(&t0);
86  for (unsigned int i = 0u; i < nbloop; i++ ) {
87  for (unsigned int j = 0u; j < nbloop; j++ ) {
88 // d = d1;
89  memcpy(&d,&d1,8);
90 // d = double(f1);
91  }
92  }
93  times(&t);
94  std::cout << "User " << float(t.tms_utime-t0.tms_utime)/sysconf(_SC_CLK_TCK)
95  << " second, System " << float(t.tms_stime-t0.tms_stime)/sysconf(_SC_CLK_TCK)
96  << " seconds" << std::endl;
97 }
98 
99 
100 int main(int argc, char **argv)
101 {
102  const int nbtest = 2;
103  if ( argc == 1 )
104  std::cout << " Usage is : \n\t"
105  << argv[0] << " i \n i being a integer from 1 to "<< nbtest << std::endl;
106 
107  int nwhat = 0; unsigned int number = 100000u;
108  if ( argc > 1 ) {
109  std::istringstream what(argv[1]); what >> nwhat;
110  }
111  if ( argc > 2 ) {
112  std::istringstream what(argv[2]); what >> number;
113  }
114 
115  switch( nwhat ) {
116  case 1:
117  cast_cost(number);
118  break;
119  case 2:
120  memcpy_cost(number);
121  break;
122  default:
123  std::cout << " Unknown test number " << nwhat << std::endl;
124  }
125 
126  return 0;
127 }
128 
129 
130 
int main(int argc, char **argv)
Definition: Benchmark.C:100
void cast_cost(unsigned int nbloop=100000u)
Definition: Benchmark.C:15
void memcpy_cost(unsigned int nbloop=100000u)
Definition: Benchmark.C:63
ADF::LogMessage & endl(ADF::LogMessage &log)