Added comparison to boost::pool memory allocator.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@325 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
b8142d7acc
commit
b078e019be
3 changed files with 471 additions and 19 deletions
|
@ -25,6 +25,10 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
#include <boost\pool\object_pool.hpp>
|
||||
#endif
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -44,12 +48,70 @@ struct Base<void> : public ThisIsASmallObject {};
|
|||
|
||||
typedef Base<void>
|
||||
A;
|
||||
typedef Base<Loki::SmallObject<> >
|
||||
typedef Base< Loki::SmallObject< Loki::SingleThreaded > >
|
||||
B;
|
||||
typedef Base<Loki::SmallValueObject<> >
|
||||
typedef Base< Loki::SmallValueObject< Loki::SingleThreaded > >
|
||||
C;
|
||||
|
||||
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
|
||||
class BoostPoolNew
|
||||
{
|
||||
private:
|
||||
static boost::object_pool< BoostPoolNew > BoostPool;
|
||||
|
||||
public:
|
||||
/// Throwing single-object new throws bad_alloc when allocation fails.
|
||||
#ifdef _MSC_VER
|
||||
/// @note MSVC complains about non-empty exception specification lists.
|
||||
static void * operator new ( std::size_t )
|
||||
#else
|
||||
static void * operator new ( std::size_t ) throw ( std::bad_alloc )
|
||||
#endif
|
||||
{
|
||||
return BoostPool.malloc();
|
||||
}
|
||||
|
||||
/// Non-throwing single-object new returns NULL if allocation fails.
|
||||
static void * operator new ( std::size_t, const std::nothrow_t & ) throw ()
|
||||
{
|
||||
return BoostPool.malloc();
|
||||
}
|
||||
|
||||
/// Placement single-object new merely calls global placement new.
|
||||
inline static void * operator new ( std::size_t size, void * place )
|
||||
{
|
||||
return ::operator new( size, place );
|
||||
}
|
||||
|
||||
/// Single-object delete.
|
||||
static void operator delete ( void * p ) throw ()
|
||||
{
|
||||
BoostPool.free( reinterpret_cast< BoostPoolNew * >( p ) );
|
||||
}
|
||||
|
||||
/** Non-throwing single-object delete is only called when nothrow
|
||||
new operator is used, and the constructor throws an exception.
|
||||
*/
|
||||
static void operator delete ( void * p, const std::nothrow_t & ) throw()
|
||||
{
|
||||
BoostPool.free( reinterpret_cast< BoostPoolNew * >( p ) );
|
||||
}
|
||||
|
||||
/// Placement single-object delete merely calls global placement delete.
|
||||
inline static void operator delete ( void * p, void * place )
|
||||
{
|
||||
::operator delete ( p, place );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
boost::object_pool< BoostPoolNew > BoostPoolNew::BoostPool;
|
||||
|
||||
typedef Base< BoostPoolNew > D;
|
||||
#endif
|
||||
|
||||
/*
|
||||
class A
|
||||
{ int i; int* p;};
|
||||
|
@ -61,7 +123,7 @@ class C : public Loki::SmallValueObject<>
|
|||
|
||||
|
||||
template<class T>
|
||||
int run_new_delete(int loop, Timer& t, char* s)
|
||||
int run_new_delete(int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -77,7 +139,7 @@ int run_new_delete(int loop, Timer& t, char* s)
|
|||
}
|
||||
|
||||
template<class T>
|
||||
int run_new_delete(T** array, int N, int loop, Timer& t, char* s)
|
||||
int run_new_delete(T** array, int N, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -94,7 +156,7 @@ int run_new_delete(T** array, int N, int loop, Timer& t, char* s)
|
|||
}
|
||||
|
||||
template<class T>
|
||||
int run_new(T** array, int loop, Timer& t, char* s)
|
||||
int run_new(T** array, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -106,7 +168,7 @@ int run_new(T** array, int loop, Timer& t, char* s)
|
|||
return t.t();
|
||||
}
|
||||
template<class T>
|
||||
int run_delete(T** array, int loop, Timer& t, char* s)
|
||||
int run_delete(T** array, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -120,7 +182,7 @@ int run_delete(T** array, int loop, Timer& t, char* s)
|
|||
|
||||
|
||||
template<class T>
|
||||
int run_new_delete_array(int N, int loop, Timer& t, char* s)
|
||||
int run_new_delete_array(int N, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -136,7 +198,7 @@ int run_new_delete_array(int N, int loop, Timer& t, char* s)
|
|||
}
|
||||
|
||||
template<class T>
|
||||
int run_new_array( int N, T** array, int loop, Timer& t, char* s)
|
||||
int run_new_array( int N, T** array, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -148,7 +210,7 @@ int run_new_array( int N, T** array, int loop, Timer& t, char* s)
|
|||
return t.t();
|
||||
}
|
||||
template<class T>
|
||||
int run_delete_array( T** array, int loop, Timer& t, char* s)
|
||||
int run_delete_array( T** array, int loop, Timer& t, const char* s)
|
||||
{
|
||||
t.start();
|
||||
/****************************************************************/
|
||||
|
@ -163,21 +225,29 @@ int run_delete_array( T** array, int loop, Timer& t, char* s)
|
|||
|
||||
|
||||
int main()
|
||||
{
|
||||
int loop = 1000000
|
||||
;
|
||||
|
||||
std::cout << "\n";
|
||||
|
||||
{
|
||||
int loop = 1000000;
|
||||
|
||||
cout << "Small-Object Benchmark Tests" << endl;
|
||||
cout << "A = global new and delete \tsizeof(A) =" << sizeof(A) << endl;
|
||||
cout << "B = Loki::SmallObject \tsizeof(B) =" << sizeof(B) << endl;
|
||||
cout << "C = Loki::SmallValueObject\tsizeof(C) =" << sizeof(C) << endl;
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
cout << "D = boost::object_pool \tsizeof(D) =" << sizeof(D) << endl;
|
||||
#endif
|
||||
cout << endl << endl;
|
||||
|
||||
Timer t;
|
||||
|
||||
t.t100 = 0;
|
||||
t.t100 = run_new_delete<A>(loop,t,"new & delete A : ");
|
||||
run_new_delete<B>(loop,t,"new & delete B : ");
|
||||
run_new_delete<C>(loop,t,"new & delete C : ");
|
||||
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_new_delete<D>(loop,t,"new & delete D : ");
|
||||
#endif
|
||||
|
||||
cout << endl << endl;
|
||||
// Loki::AllocatorSingleton<>::ClearExtraMemory();
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int N = 100000;
|
||||
|
@ -186,21 +256,29 @@ int main()
|
|||
A** a = new A*[N];
|
||||
B** b = new B*[N];
|
||||
C** c = new C*[N];
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
D** d = new D*[N];
|
||||
#endif
|
||||
|
||||
for(int i=0; i<N; i++)
|
||||
{
|
||||
a[i]=0;
|
||||
b[i]=0;
|
||||
c[i]=0;
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
d[i]=0;
|
||||
#endif
|
||||
}
|
||||
|
||||
t.t100 = 0;
|
||||
t.t100 = run_new_delete(a,N,loop2,t,"new & del. A on array: ");
|
||||
run_new_delete(b,N,loop2,t,"new & del. B on array: ");
|
||||
run_new_delete(c,N,loop2,t,"new & del. C on array: ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_new_delete(d,N,loop2,t,"new & del. D on array: ");
|
||||
#endif
|
||||
|
||||
cout << endl << endl;
|
||||
// Loki::AllocatorSingleton<>::ClearExtraMemory();
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
@ -208,6 +286,9 @@ int main()
|
|||
t.t100 = run_new(a,N,t,"new A on array : ");
|
||||
run_new(b,N,t,"new B on array : ");
|
||||
run_new(c,N,t,"new C on array : ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_new(d,N,t,"new D on array : ");
|
||||
#endif
|
||||
|
||||
cout << endl;
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -216,9 +297,11 @@ int main()
|
|||
t.t100 = run_delete(a,N,t,"delete A on array : ");
|
||||
run_delete(b,N,t,"delete B on array : ");
|
||||
run_delete(c,N,t,"delete C on array : ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_delete(d,N,t,"delete D on array : ");
|
||||
#endif
|
||||
|
||||
cout << endl << endl;
|
||||
// Loki::AllocatorSingleton<>::ClearExtraMemory();
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
@ -228,6 +311,9 @@ int main()
|
|||
t.t100 = run_new_delete_array<A>(N,loop,t,"new & delete [] A : ");
|
||||
run_new_delete_array<B>(N,loop,t,"new & delete [] B : ");
|
||||
run_new_delete_array<C>(N,loop,t,"new & delete [] C : ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_new_delete_array<D>(N,loop,t,"new & delete [] D : ");
|
||||
#endif
|
||||
|
||||
cout << endl << endl;
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -238,6 +324,9 @@ int main()
|
|||
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,c,count,t,"new [] C on array : ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_new_array(N,d,count,t,"new [] D on array : ");
|
||||
#endif
|
||||
|
||||
cout << endl;
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -246,11 +335,17 @@ int main()
|
|||
t.t100 = run_delete_array(a,count,t,"delete [] A on array : ");
|
||||
run_delete_array(b,count,t,"delete [] B on array : ");
|
||||
run_delete_array(c,count,t,"delete [] C on array : ");
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
run_delete_array(d,count,t,"delete [] D on array : ");
|
||||
#endif
|
||||
|
||||
|
||||
delete [] a;
|
||||
delete [] b;
|
||||
delete [] c;
|
||||
#ifdef COMPARE_BOOST_POOL
|
||||
delete [] d;
|
||||
#endif
|
||||
|
||||
cout << endl << endl;
|
||||
Loki::AllocatorSingleton<>::ClearExtraMemory();
|
||||
|
@ -265,6 +360,9 @@ int main()
|
|||
// ----------------------------------------------------------------------------
|
||||
|
||||
// $Log$
|
||||
// Revision 1.8 2005/10/26 00:41:00 rich_sposato
|
||||
// Added comparison to boost::pool memory allocator.
|
||||
//
|
||||
// Revision 1.7 2005/10/14 18:35:06 rich_sposato
|
||||
// Added cvs keywords.
|
||||
//
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue