make object size more flexible

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@327 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-10-26 23:30:06 +00:00
parent 841e406282
commit 4978cfb9d8

View file

@ -14,10 +14,10 @@
// $Header$ // $Header$
//#define LOKI_CLASS_LEVEL_THREADING //#define LOKI_CLASS_LEVEL_THREADING
//#define LOKI_OBJECT_LEVEL_THREADING #define LOKI_OBJECT_LEVEL_THREADING
// Uncomment this to test new [] and delete []. // Uncomment this to test new [] and delete [].
//#define LOKI_SMALL_OBJECT_USE_NEW_ARRAY #define LOKI_SMALL_OBJECT_USE_NEW_ARRAY
#include "SmallObj.h" #include "SmallObj.h"
#include "timer.h" #include "timer.h"
@ -25,6 +25,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#define COMPARE_BOOST_POOL
#ifdef COMPARE_BOOST_POOL #ifdef COMPARE_BOOST_POOL
#include <boost\pool\object_pool.hpp> #include <boost\pool\object_pool.hpp>
#endif #endif
@ -33,33 +34,26 @@
using namespace std; using namespace std;
template<unsigned int N>
class ThisIsASmallObject class ThisIsASmallObject
{ {
int i; char data[N];
double d;
//std::string s;
}; };
template<class T> template<unsigned int N, class T>
struct Base : public ThisIsASmallObject, public T {}; struct Base : public ThisIsASmallObject<N>, public T {};
template<>
struct Base<void> : public ThisIsASmallObject {};
typedef Base<void> template<unsigned int N>
A; struct Base<N, void> : public ThisIsASmallObject<N> {};
typedef Base< Loki::SmallObject< Loki::SingleThreaded > >
B;
typedef Base< Loki::SmallValueObject< Loki::SingleThreaded > >
C;
#ifdef COMPARE_BOOST_POOL #ifdef COMPARE_BOOST_POOL
class BoostPoolNew template<unsigned int N>
class BoostPoolNew : public Base<N,void>
{ {
private: private:
static boost::object_pool< BoostPoolNew > BoostPool; static boost::object_pool< BoostPoolNew<N> > BoostPool;
public: public:
/// Throwing single-object new throws bad_alloc when allocation fails. /// Throwing single-object new throws bad_alloc when allocation fails.
@ -107,21 +101,28 @@ public:
}; };
boost::object_pool< BoostPoolNew > BoostPoolNew::BoostPool; template<unsigned int N>
boost::object_pool< BoostPoolNew<N> > BoostPoolNew<N>::BoostPool;
typedef Base< BoostPoolNew > D;
#endif #endif
/*
class A
{ int i; int* p;};
class B : public Loki::SmallObject<>
{ int i; int* p;};
class C : public Loki::SmallValueObject<>
{ int i; int* p;};
*/
template<class T>
int run_oneline_new_delete(int loop, Timer& t, const char* s)
{
t.start();
/****************************************************************/
for (int i=0; i<loop; ++i)
{
delete new T;
}
/****************************************************************/
t.stop();
t.print(t.t(),s);
return t.t();
}
template<class T> template<class T>
int run_new_delete(int loop, Timer& t, const char* s) int run_new_delete(int loop, Timer& t, const char* s)
{ {
@ -224,11 +225,21 @@ int run_delete_array( T** array, int loop, Timer& t, const char* s)
int main() template<unsigned int N, int loop>
void testSize()
{ {
int loop = 1000000; typedef Base<N, void>
A;
typedef Base<N, Loki::SmallObject< Loki::SingleThreaded > >
B;
typedef Base<N, Loki::SmallValueObject< Loki::SingleThreaded > >
C;
#ifdef COMPARE_BOOST_POOL
typedef BoostPoolNew<N>
D;
#endif
cout << "Small-Object Benchmark Tests" << endl; cout << "Small-Object Benchmark Tests \n" << endl;
cout << "A = global new and delete \tsizeof(A) =" << sizeof(A) << endl; cout << "A = global new and delete \tsizeof(A) =" << sizeof(A) << endl;
cout << "B = Loki::SmallObject \tsizeof(B) =" << sizeof(B) << endl; cout << "B = Loki::SmallObject \tsizeof(B) =" << sizeof(B) << endl;
cout << "C = Loki::SmallValueObject\tsizeof(C) =" << sizeof(C) << endl; cout << "C = Loki::SmallValueObject\tsizeof(C) =" << sizeof(C) << endl;
@ -239,6 +250,17 @@ int main()
Timer t; Timer t;
t.t100 = 0;
t.t100 = run_oneline_new_delete<A>(loop,t,"'delete new A' : ");
run_new_delete<B>(loop,t,"'delete new B' : ");
run_new_delete<C>(loop,t,"'delete new C' : ");
#ifdef COMPARE_BOOST_POOL
run_new_delete<D>(loop,t,"'delete new D' : ");
#endif
cout << endl << endl;
////////////////////////////////////////////////////////////////////////////////
t.t100 = 0; t.t100 = 0;
t.t100 = run_new_delete<A>(loop,t,"new & delete A : "); t.t100 = run_new_delete<A>(loop,t,"new & delete A : ");
run_new_delete<B>(loop,t,"new & delete B : "); run_new_delete<B>(loop,t,"new & delete B : ");
@ -250,7 +272,7 @@ int main()
cout << endl << endl; cout << endl << endl;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int N = 100000; int N = 10000;
int loop2 = loop/N*10; int loop2 = loop/N*10;
A** a = new A*[N]; A** a = new A*[N];
@ -319,7 +341,7 @@ int main()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int count = 1000; int count = 100;
t.t100 = 0; t.t100 = 0;
t.t100 = run_new_array(N,a,count,t,"new [] A on array : "); t.t100 = run_new_array(N,a,count,t,"new [] A on array : ");
run_new_array(N,b,count,t,"new [] B on array : "); run_new_array(N,b,count,t,"new [] B on array : ");
@ -350,19 +372,36 @@ int main()
cout << endl << endl; cout << endl << endl;
Loki::AllocatorSingleton<>::ClearExtraMemory(); Loki::AllocatorSingleton<>::ClearExtraMemory();
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
}
int main()
{
cout << endl; cout << endl;
const int loop = 700000;
testSize<8,loop>();
testSize<64,loop>();
//testSize<256,loop>();
//testSize<1024,loop>();
system("PAUSE"); system("PAUSE");
return 0; return 0;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// $Log$ // $Log$
// Revision 1.9 2005/10/26 23:30:06 syntheticpp
// make object size more flexible
//
// Revision 1.8 2005/10/26 00:41:00 rich_sposato // Revision 1.8 2005/10/26 00:41:00 rich_sposato
// Added comparison to boost::pool memory allocator. // Added comparison to boost::pool memory allocator.
// //
// Revision 1.7 2005/10/14 18:35:06 rich_sposato // Revision 1.7 2005/10/14 18:35:06 rich_sposato
// Added cvs keywords. // Added cvs keywords.
// //