1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
12 #include <anna/core/core.hpp>
13 #include <anna/timex/MicroMeter.hpp>
17 static const int N = 10000;
19 void print_const (const Recycler<int>& numbers)
24 for (Recycler <int>::const_iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) {
25 c = Recycler <int>::data (ii);
26 msg = anna::functions::asString ("Number 3 (p=0x%x): %d", c, *c);
27 Logger::debug (msg, ANNA_FILE_LOCATION);
31 void debug (const char* str, Recycler<int>::iterator ii)
34 int* p = Recycler<int>::data (ii);
38 msg += functions::asHexString (anna_ptrnumber_cast (p));
39 msg += functions::asText ("): ", *p);
40 Logger::debug (msg, ANNA_FILE_LOCATION);
43 void run (const bool randomAccess, const int freeCounter)
44 throw (RuntimeException)
46 Recycler <int> numbers (randomAccess);
52 for (i = 0; i < N; i ++) {
53 p = numbers.create ();
58 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++)
59 debug ("Number 1", ii);
61 // Calcule cuantos objetos tiene que liberar
64 for (int nn = 0; nn < freeCounter; nn ++) {
65 number = rand () % (N * 2);
67 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) {
68 p = Recycler <int>::data (ii);
71 debug ("Release | ", ii);
78 for (i = 0; i < N; i ++) {
79 p = numbers.create ();
82 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++)
83 debug ("Number 2", ii);
86 for (i = 0; i < N; i ++) {
87 p = numbers.create ();
91 print_const (numbers);
94 void recycling (const bool randomAccess, const int freeCounter)
95 throw (RuntimeException)
97 Recycler <int> numbers (randomAccess);
105 for (i = 0; i < N; i ++) {
106 p = pp [i] = numbers.create ();
110 // How many objects must be released?
113 for (int nn = 0; nn < freeCounter; nn ++) {
114 number = rand () % N;
120 void reuse (const int freeCounter)
121 throw (RuntimeException)
130 for (i = 0; i < N; i ++) {
131 p = pp [i] = new int;
135 // How many objects must be released?
138 for (int nn = 0; nn < freeCounter; nn ++) {
139 number = rand () % N;
145 int main (int argc, const char** argv)
147 anna::timex::MicroMeter meter;
150 Logger::setLevel (Logger::Debug);
151 Logger::initialize ("recycler", new TraceWriter ("file.trace", 2048000));
153 const int freeCounter = rand () % N;
155 Logger::debug (functions::asText ("Nodes to delete: ", freeCounter), ANNA_FILE_LOCATION);
157 cout << "Nodes to delete: " << freeCounter << endl << endl;
159 run (false, freeCounter);
160 cout << "run (false, freeCounter): " << meter.getDelay () << " us" << endl;
162 meter.setControlPoint ();
163 run (true, freeCounter);
164 cout << "run (true, freeCounter): " << meter.getDelay () << " us" << endl;
166 meter.setControlPoint ();
167 recycling (false, freeCounter);
168 cout << "recycling(false): " << meter.getDelay () << " us" << endl;
170 meter.setControlPoint ();
171 recycling (true, freeCounter);
172 cout << "recycling(true): " << meter.getDelay () << " us" << endl;
174 meter.setControlPoint ();
176 cout << "reuse(): " << meter.getDelay () << " us" << endl;
178 catch (Exception& ex) {
179 cout << ex.asString () << endl;
182 Recycler <string> rstr;
183 meter.setControlPoint ();