2008-08-08 00:43:20 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/benchmarks/strings.cpp
|
|
|
|
// Purpose: String-related benchmarks
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-07-19
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "bench.h"
|
|
|
|
|
2008-08-08 02:59:32 +00:00
|
|
|
#include "wx/tls.h"
|
|
|
|
|
2008-08-08 01:13:30 +00:00
|
|
|
#if defined(__UNIX__)
|
2008-08-08 00:43:20 +00:00
|
|
|
#define HAVE_PTHREAD
|
|
|
|
#include <pthread.h>
|
2008-08-08 01:13:30 +00:00
|
|
|
#elif defined(__WIN32__)
|
|
|
|
#define HAVE_WIN32_THREAD
|
|
|
|
#include "wx/msw/wrapwin.h"
|
2008-08-08 00:43:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if wxCHECK_GCC_VERSION(3, 3)
|
|
|
|
#define HAVE_COMPILER_THREAD
|
|
|
|
#define wxTHREAD_SPECIFIC __thread
|
2008-08-08 01:13:30 +00:00
|
|
|
#elif wxCHECK_VISUALC_VERSION(7)
|
|
|
|
#define HAVE_COMPILER_THREAD
|
|
|
|
#define wxTHREAD_SPECIFIC __declspec(thread)
|
2008-08-08 00:43:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// uncomment this to also test Boost version (you will also need to link with
|
|
|
|
// libboost_threads)
|
2008-08-13 15:47:20 +00:00
|
|
|
//#define HAVE_BOOST_THREAD
|
2008-08-08 00:43:20 +00:00
|
|
|
#ifdef HAVE_BOOST_THREAD
|
|
|
|
#include <boost/thread/tss.hpp>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static const int NUM_ITER = 1000;
|
|
|
|
|
|
|
|
// this is just a baseline
|
|
|
|
BENCHMARK_FUNC(DummyTLS)
|
|
|
|
{
|
|
|
|
static int s_global = 0;
|
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
s_global = 0;
|
|
|
|
else
|
|
|
|
s_global = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !s_global;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_COMPILER_THREAD
|
|
|
|
|
|
|
|
BENCHMARK_FUNC(CompilerTLS)
|
|
|
|
{
|
|
|
|
static wxTHREAD_SPECIFIC int s_global = 0;
|
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
s_global = 0;
|
|
|
|
else
|
|
|
|
s_global = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !s_global;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_COMPILER_THREAD
|
|
|
|
|
|
|
|
#ifdef HAVE_PTHREAD
|
|
|
|
|
|
|
|
class PthreadKey
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PthreadKey()
|
|
|
|
{
|
|
|
|
pthread_key_create(&m_key, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PthreadKey()
|
|
|
|
{
|
|
|
|
pthread_key_delete(m_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator pthread_key_t() const { return m_key; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_key_t m_key;
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(PthreadKey)
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_FUNC(PosixTLS)
|
|
|
|
{
|
|
|
|
static PthreadKey s_key;
|
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
pthread_setspecific(s_key, 0);
|
|
|
|
else
|
|
|
|
pthread_setspecific(s_key, &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !pthread_getspecific(s_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_PTHREAD
|
|
|
|
|
2008-08-08 01:13:30 +00:00
|
|
|
#ifdef HAVE_WIN32_THREAD
|
|
|
|
|
|
|
|
class TlsSlot
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TlsSlot()
|
|
|
|
{
|
|
|
|
m_slot = ::TlsAlloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
~TlsSlot()
|
|
|
|
{
|
|
|
|
::TlsFree(m_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator DWORD() const { return m_slot; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DWORD m_slot;
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(TlsSlot)
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_FUNC(Win32TLS)
|
|
|
|
{
|
|
|
|
static TlsSlot s_slot;
|
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
::TlsSetValue(s_slot, 0);
|
|
|
|
else
|
|
|
|
::TlsSetValue(s_slot, &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !::TlsGetValue(s_slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_WIN32_THREAD
|
|
|
|
|
2008-08-08 00:43:20 +00:00
|
|
|
#ifdef HAVE_BOOST_THREAD
|
|
|
|
|
|
|
|
BENCHMARK_FUNC(BoostTLS)
|
|
|
|
{
|
|
|
|
static boost::thread_specific_ptr<int> s_ptr;
|
|
|
|
if ( !s_ptr.get() )
|
|
|
|
s_ptr.reset(new int(0));
|
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
*s_ptr = 0;
|
|
|
|
else
|
|
|
|
*s_ptr = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !*s_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_BOOST_THREAD
|
2008-08-08 02:59:32 +00:00
|
|
|
|
|
|
|
BENCHMARK_FUNC(wxTLS)
|
|
|
|
{
|
2008-08-29 23:28:42 +00:00
|
|
|
static wxTLS_TYPE(int) s_globalVar;
|
|
|
|
#define s_global wxTLS_VALUE(s_globalVar)
|
2008-08-08 02:59:32 +00:00
|
|
|
|
|
|
|
for ( int n = 0; n < NUM_ITER; n++ )
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
|
|
|
s_global = 0;
|
|
|
|
else
|
|
|
|
s_global = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !s_global;
|
|
|
|
}
|