Jpp
JAllocator.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <algorithm>
5 
6 #include "JLang/JAllocator.hh"
7 #include "JLang/JRAM.hh"
9 
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 #include "Jeep/JTimer.hh"
13 
14 
15 namespace {
16 
17  struct __A__
18  {
19  __A__() :
20  i(0),
21  d(0.0)
22  {}
23 
24  int i;
25  double d;
26  };
27 
28 
29  struct __B__ :
30  public __A__,
31  public JLANG::JObjectAllocator<__A__, JLANG::JAllocator>
32  {};
33 
34 
35  struct __C__ :
36  public __A__,
37  public JLANG::JObjectAllocator<__A__, JLANG::JRAM>
38  {};
39 
40 
41  /**
42  * Test method for memory allocation and de-allocation.
43  *
44  * \param argc number of command line arguments
45  * \param argv list of command line arguments
46  * \param title title of this template process
47  */
48  template<class JType_t>
49  int do_main(int argc, char **argv, const char* title)
50  {
51  using namespace std;
52 
53  int numberOfAllocs;
54  int numberOfEvents;
55  bool random;
56  bool reverse;
57  int debug;
58 
59  try {
60 
61  JParser<> zap("Auxiliary program to test speed of memory allocation.");
62 
63  zap['n'] = make_field(numberOfAllocs) = 1000;
64  zap['N'] = make_field(numberOfEvents) = 1000;
65  zap['r'] = make_field(random);
66  zap['R'] = make_field(reverse);
67  zap['d'] = make_field(debug) = 3;
68 
69  zap(argc, argv);
70  }
71  catch(const exception &error) {
72  FATAL(error.what() << endl);
73  }
74 
75 
76  using namespace JPP;
77 
78  cout << endl << title << endl;
79 
80  JTimer timer1("Allocate");
81  JTimer timer2("Free");
82 
83  timer1.reset();
84  timer2.reset();
85 
86 
87  vector<JType_t*> buffer;
88 
89  for (int event_count = 0; event_count != numberOfEvents; ++event_count) {
90 
91  buffer.resize(numberOfAllocs);
92 
93  typename vector<JType_t*>::iterator out = buffer.begin();
94 
95  timer1.start();
96 
97  for (int i = numberOfAllocs; i != 0; --i, ++out) {
98  *out = new JType_t();
99  }
100 
101  timer1.stop();
102 
103  if (random) {
104  random_shuffle(buffer.begin(), buffer.end());
105  }
106 
107  if (reverse) {
108  std::reverse (buffer.begin(), buffer.end());
109  }
110 
111  timer2.start();
112 
113  for (typename vector<JType_t*>::iterator i = buffer.begin(); i != buffer.end(); ++i) {
114  delete *i;
115  }
116 
117  timer2.stop();
118  }
119 
120  const double factor = 1.0 / (numberOfEvents * numberOfAllocs);
121 
122  timer1.print(cout, factor, nano_t);
123  timer2.print(cout, factor, nano_t);
124 
125  return 0;
126  }
127 }
128 
129 /**
130  * \file
131  *
132  * Auxiliary program to test speed of JLANG::JObjectAllocator class.
133  * \author mdejong
134  */
135 int main(int argc, char **argv)
136 {
137  if (do_main<__A__>(argc, argv, "C++") != 0) return 1;
138  if (do_main<__B__>(argc, argv, "JAllocator") != 0) return 1;
139  if (do_main<__C__>(argc, argv, "JRAM") != 0) return 1;
140 }
JLANG::JObjectAllocator
Base class for customized new/delete operators.
Definition: JObjectAllocator.hh:29
JMessage.hh
JObjectAllocator.hh
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
main
int main(int argc, char **argv)
Definition: JAllocator.cc:135
debug
int debug
debug level
Definition: JSirene.cc:59
JEEP::nano_t
nano
Definition: JScale.hh:28
JAllocator.hh
JParser.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
std
Definition: jaanetDictionary.h:36
JTimer.hh
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JRAM.hh