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