Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * \return status
48  */
49  template<class JType_t>
50  int do_main(int argc, char **argv, const char* title)
51  {
52  using namespace std;
53 
54  int numberOfAllocs;
55  int numberOfEvents;
56  bool random;
57  bool reverse;
58  int debug;
59 
60  try {
61 
62  JParser<> zap("Auxiliary program to test speed of memory allocation.");
63 
64  zap['n'] = make_field(numberOfAllocs) = 1000;
65  zap['N'] = make_field(numberOfEvents) = 1000;
66  zap['r'] = make_field(random);
67  zap['R'] = make_field(reverse);
68  zap['d'] = make_field(debug) = 3;
69 
70  zap(argc, argv);
71  }
72  catch(const exception &error) {
73  FATAL(error.what() << endl);
74  }
75 
76 
77  using namespace JPP;
78 
79  cout << endl << title << endl;
80 
81  JTimer timer1("Allocate");
82  JTimer timer2("Free");
83 
84  timer1.reset();
85  timer2.reset();
86 
87 
88  vector<JType_t*> buffer;
89 
90  for (int event_count = 0; event_count != numberOfEvents; ++event_count) {
91 
92  buffer.resize(numberOfAllocs);
93 
94  typename vector<JType_t*>::iterator out = buffer.begin();
95 
96  timer1.start();
97 
98  for (int i = numberOfAllocs; i != 0; --i, ++out) {
99  *out = new JType_t();
100  }
101 
102  timer1.stop();
103 
104  if (random) {
105  random_shuffle(buffer.begin(), buffer.end());
106  }
107 
108  if (reverse) {
109  std::reverse (buffer.begin(), buffer.end());
110  }
111 
112  timer2.start();
113 
114  for (typename vector<JType_t*>::iterator i = buffer.begin(); i != buffer.end(); ++i) {
115  delete *i;
116  }
117 
118  timer2.stop();
119  }
120 
121  const double factor = 1.0 / (numberOfEvents * numberOfAllocs);
122 
123  timer1.print(cout, factor, nano_t);
124  timer2.print(cout, factor, nano_t);
125 
126  return 0;
127  }
128 }
129 
130 /**
131  * \file
132  *
133  * Auxiliary program to test speed of JLANG::JObjectAllocator class.
134  * \author mdejong
135  */
136 int main(int argc, char **argv)
137 {
138  if (do_main<__A__>(argc, argv, "C++") != 0) return 1;
139  if (do_main<__B__>(argc, argv, "JAllocator") != 0) return 1;
140  if (do_main<__C__>(argc, argv, "JRAM") != 0) return 1;
141 }
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
Auxiliary base class to speed up new/delete operations of any class.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
int debug
debug level
Definition: JSirene.cc:63
Auxiliary class to speed up new/delete operations of any class.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
nano
Definition: JScale.hh:28
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
Utility class to parse command line options.
Auxiliary class to speed up new/delete operations of any class.
Base class for customized new/delete operators.