#include #include #include #include #include #include "JLang/JAllocator.hh" #include "JLang/JRAM.hh" #include "JLang/JObjectAllocator.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" #include "Jeep/JTimer.hh" namespace { struct __A__ { __A__() : i(0), d(0.0) {} int i; double d; }; struct __B__ : public __A__, public JLANG::JObjectAllocator<__A__, JLANG::JAllocator> {}; struct __C__ : public __A__, public JLANG::JObjectAllocator<__A__, JLANG::JRAM> {}; /** * Test method for memory allocation and de-allocation. * * \param argc number of command line arguments * \param argv list of command line arguments * \param title title of this template process * \return status */ template int do_main(int argc, char **argv, const char* title) { using namespace std; int numberOfAllocs; int numberOfEvents; bool random; bool reverse; int debug; try { JParser<> zap("Auxiliary program to test speed of memory allocation."); zap['n'] = make_field(numberOfAllocs) = 1000; zap['N'] = make_field(numberOfEvents) = 1000; zap['r'] = make_field(random); zap['R'] = make_field(reverse); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } using namespace JPP; cout << endl << title << endl; JTimer timer1("Allocate"); JTimer timer2("Free"); timer1.reset(); timer2.reset(); vector buffer; for (int event_count = 0; event_count != numberOfEvents; ++event_count) { buffer.resize(numberOfAllocs); typename vector::iterator out = buffer.begin(); timer1.start(); for (int i = numberOfAllocs; i != 0; --i, ++out) { *out = new JType_t(); } timer1.stop(); if (random) { random_device rd; mt19937 g(rd()); std::shuffle(buffer.begin(), buffer.end(), g); } if (reverse) { std::reverse(buffer.begin(), buffer.end()); } timer2.start(); for (typename vector::iterator i = buffer.begin(); i != buffer.end(); ++i) { delete *i; } timer2.stop(); } const double factor = 1.0 / (numberOfEvents * numberOfAllocs); timer1.print(cout, factor, nano_t); timer2.print(cout, factor, nano_t); return 0; } } /** * \file * * Auxiliary program to test speed of JLANG::JObjectAllocator class. * \author mdejong */ int main(int argc, char **argv) { if (do_main<__A__>(argc, argv, "C++") != 0) return 1; if (do_main<__B__>(argc, argv, "JAllocator") != 0) return 1; if (do_main<__C__>(argc, argv, "JRAM") != 0) return 1; }