From cb9492fca91261d9b8c6fd9730fbba6a86f99e5b Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Thu, 27 Oct 2005 19:11:01 +0000 Subject: [PATCH] new bechmark with: new, SmallObj, SmallValueObj, boost::object_pool, malloc and std::allocator git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@329 7ec92016-0320-0410-acc4-a06ded1c099a --- test/SmallObj/AllocatorBench.cpp | 298 +++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100755 test/SmallObj/AllocatorBench.cpp diff --git a/test/SmallObj/AllocatorBench.cpp b/test/SmallObj/AllocatorBench.cpp new file mode 100755 index 0000000..5184d71 --- /dev/null +++ b/test/SmallObj/AllocatorBench.cpp @@ -0,0 +1,298 @@ +#if 0 +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (c) 2005 Peter Kümmel +// Copyright (c) 2005 Richard Sposato +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this +// permission notice appear in supporting documentation. +// The authors make no representations about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. +//////////////////////////////////////////////////////////////////////////////// + +// $Header: + +//#define LOKI_CLASS_LEVEL_THREADING +//#define LOKI_OBJECT_LEVEL_THREADING + +// Uncomment this to test new [] and delete []. +#define LOKI_SMALL_OBJECT_USE_NEW_ARRAY + +#include "SmallObj.h" +#include "timer.h" + +#include +#include + +#undef COMPARE_BOOST_POOL +//#define COMPARE_BOOST_POOL +#ifdef COMPARE_BOOST_POOL + #include +#endif + + +using namespace std; + + +template +class ThisIsASmallObject +{ + char data[N]; +}; + +template +struct Base : public ThisIsASmallObject, public T {}; + +template +struct Base : public ThisIsASmallObject {}; + + +#ifdef COMPARE_BOOST_POOL + +template +class BoostPoolNew : public Base +{ +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 ); + } + +}; + +template +boost::object_pool< BoostPoolNew > BoostPoolNew::BoostPool; + +#endif + + +int array_test_nr = 0; +double t100_new = 0; +double t100_delete = 0; + +#define LOKI_SMALLOBJ_BENCH(FUNC, CODE_LOOP) \ +template \ +int FUNC(void**, const int N, int loop, Timer& t, const char* s) \ +{ \ + t.start(); \ + /****************************************************************/ \ + for (int i=0; i \ +int FUNC(void** arrv, const int N, int loop, Timer& t, const char* s) \ +{ \ + \ + CODE_DECL; \ + T** arr = reinterpret_cast(arrv); \ + t.start(); \ + /****************************************************************/ \ + for (int i=0; i st;st.deallocate(st.allocate(1), 1);) + + +LOKI_SMALLOBJ_BENCH(delete_new_array ,delete[] new T[N];) +LOKI_SMALLOBJ_BENCH(delete_new_array_mal,std::free(std::malloc(sizeof(T[TN])));); +LOKI_SMALLOBJ_BENCH(delete_new_array_all,std::allocator st;st.deallocate(st.allocate(1), 1);) + + +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_on_arr , , arr[i] = new T; , + delete arr[i];) +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_on_arr_mal, , arr[i] = static_cast(std::malloc(sizeof(T))); , + std::free(arr[i]);) +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_on_arr_all, std::allocator st , + arr[i]=st.allocate(1); , + st.deallocate(arr[i], 1);) + +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a , , arr[i] = new T[TN]; , + delete[] arr[i];) +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a_mal, , arr[i] = static_cast(std::malloc(sizeof(T[TN]))); , + std::free(arr[i]);) +LOKI_SMALLOBJ_BENCH_ARRAY(new_del_a_on_a_all,std::allocator st , + arr[i]=reinterpret_cast(st.allocate(1)); , + st.deallocate(reinterpret_cast(arr[i]), 1);) + + + +#ifndef COMPARE_BOOST_POOL +#define LOKI_SMALLOBJBECH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \ + array_test_nr = 0; \ + std::cout << MESSAGE <<"\n"; \ + FUNC(a,N,LOOP,TIMER,"A:"); \ + FUNC(a,N,LOOP,TIMER,"B:"); \ + FUNC(a,N,LOOP,TIMER,"C:"); \ + FUNC##_all(a,N,LOOP,TIMER,"E:"); \ + FUNC##_mal(a,N,LOOP,TIMER,"M:"); \ + cout << endl << endl; +#else +#define LOKI_SMALLOBJBECH_ABCD(FUNC,N,LOOP,TIMER,MESSAGE) \ + array_test_nr = 0; \ + std::cout << MESSAGE <<"\n"; \ + FUNC(a,N,LOOP,TIMER,"A:"); \ + FUNC(a,N,LOOP,TIMER,"B:"); \ + FUNC(a,N,LOOP,TIMER,"C:"); \ + FUNC(a,N,LOOP,TIMER,"D:"); \ + FUNC##_all(a,N,LOOP,TIMER,"E:"); \ + FUNC##_mal(a,N,LOOP,TIMER,"M:"); \ + cout << endl << endl; +#endif + +template +void testSize() +{ + typedef Base A; + typedef Base > B; + typedef Base > C; +#ifdef COMPARE_BOOST_POOL + typedef BoostPoolNew D; +#endif + + cout << "Small-Object Benchmark Tests \n" << 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 << "E = std::allocator \tsizeof(D) =" << sizeof(A) << endl; + cout << "M = std::malloc/free \tsizeof(D) =" << sizeof(A) << endl; + cout << endl << endl; + + Timer t; + + const int N = 20; + int N_on_array = 1000*1000; + + void** a= new void*[N_on_array]; + + LOKI_SMALLOBJBECH_ABCD(delete_new ,0,loop,t,"'delete new T'"); + + LOKI_SMALLOBJBECH_ABCD(delete_new_array ,N,loop,t,"'delete[] new T[N]'"); + + LOKI_SMALLOBJBECH_ABCD(new_del_on_arr ,0,N_on_array,t,"1. 'arr[i] = new T' 2. 'delete arr[i]'"); + + LOKI_SMALLOBJBECH_ABCD(new_del_a_on_a ,N,N_on_array,t,"1. 'arr[i] = new T[N]' 2. 'delete[] arr[i]'"); + + + delete [] a; + + cout << endl << endl; + Loki::AllocatorSingleton<>::ClearExtraMemory(); +} + + +int main() +{ + cout << endl; + + const int loop = 1000*1000; + + //testSize<8,loop>(); + testSize<64,loop>(); + //testSize<256,loop>(); + //testSize<1024,loop>(); + + + system("PAUSE"); + + return 0; +} + +// ---------------------------------------------------------------------------- + +// $Log: + +#endif