2006-09-05 17:14:38 -04:00
/////////////////////////////////////////////////////////////////////////////
// Name: printfbench.cpp
2009-05-30 09:54:22 -04:00
// Purpose: benchmarks for wx*Printf*() functions
2006-09-05 17:14:38 -04:00
// Author: Francesco Montorsi
// Modified by:
// Created: 27/3/2006
2009-05-30 09:54:22 -04:00
// Copyright: (c) 2006-2009 Francesco Montorsi
2010-07-13 09:29:13 -04:00
// Licence: wxWindows licence
2006-09-05 17:14:38 -04:00
/////////////////////////////////////////////////////////////////////////////
2009-05-30 09:54:22 -04:00
/*
TODO : revise the benchmarking functions below to allow valid comparisons
between the wx implementation and the system ' s implementation of
the tested functions ( e . g . adding tests which use the wxS macro to
avoid runtime encoding conversions , etc etc ) .
*/
2008-03-23 14:08:16 -04:00
//
// Profiling under Linux:
// =====================
//
// 1) configure wxWidgets in release mode
// 2) make sure that HAVE_UNIX98_PRINTF is undefined (just #defining it to zero
// does not work; you must comment out the entire #define) in your setup.h;
// and also that wxUSE_PRINTF_POS_PARAMS is set to 1; this will force the
// use of wx's own implementation of wxVsnprintf()
// 3) compile wx
// 4) set wxTEST_WX_ONLY to 1 and compile tests as well
//
// Now you have two main choices:
//
// - using gprof:
// 5) add to the Makefile of this test program the -pg option both to
// CXXFLAGS and to LDFLAGS
// 6) run the test
// 7) look at the gmon.out file with gprof utility
//
// - using valgrind:
// 4) run "valgrind --tool=callgrind ./printfbench"
// 5) run "kcachegrind dump_file_generated_by_callgrind"
//
2009-05-30 09:54:22 -04:00
# include "wx/string.h"
# include "bench.h"
2006-09-05 17:14:38 -04:00
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
# define BUFSIZE 10000
const wxString g_verylongString =
2009-05-30 09:54:22 -04:00
" very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very "
" very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long string! \n \n \n " ;
2006-09-05 17:14:38 -04:00
// ----------------------------------------------------------------------------
// benchmarking helpers
// ----------------------------------------------------------------------------
2009-05-30 09:54:22 -04:00
# define DO_LONG_BENCHMARK(fnc, prefix) \
fnc ( buffer , BUFSIZE , \
prefix # # " This is a reasonably long string with various %s arguments, exactly %d, " \
2009-06-01 08:40:50 -04:00
prefix # # " and is used as benchmark for %s - %% %.2f %d %s " , \
prefix # # " (many!!) " , 6 , " this program " , 23.342f , 999 , \
2009-05-30 09:54:22 -04:00
( const char * ) g_verylongString . c_str ( ) ) ;
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
# define DO_LONG_POSITIONAL_BENCHMARK(fnc, prefix) \
fnc ( buffer , BUFSIZE , \
prefix # # " This is a %2$s and thus is harder to parse... let's %1$s " \
2009-06-01 08:40:50 -04:00
prefix # # " for our benchmarking aims - %% %3$f %5$d %4$s " , \
prefix # # " test it " , " string with positional arguments " , 23.342f , \
2009-05-30 09:54:22 -04:00
( const char * ) g_verylongString . c_str ( ) , 999 ) ;
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
# define DO_BENCHMARK(fnc, prefix) \
fnc ( buffer , BUFSIZE , prefix # # " This is a short %s string with very few words " , " test " ) ;
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
# define DO_POSITIONAL_BENCHMARK(fnc, prefix) \
fnc ( buffer , BUFSIZE , \
prefix # # " This is a %2$s and thus is harder to parse... nonetheless, %1$s ! " , \
" test it " , " string with positional arguments " ) ;
2006-09-05 17:14:38 -04:00
// the configure script of wxWidgets will define HAVE_UNIX98_PRINTF on those
// system with a *printf() family of functions conformant to Unix 98 standard;
// systems without the configure script as build system (e.g. Windows) do not
// have positional support anyway
# ifdef HAVE_UNIX98_PRINTF
# define wxSYSTEM_HAS_POSPARAM_SUPPORT 1
# else
# define wxSYSTEM_HAS_POSPARAM_SUPPORT 1
# endif
// we need to avoid the use of wxPrintf() here since it could have been mapped
// to wxWidgets' implementation of wxVsnPrintf() !
# if wxUSE_UNICODE
# define sys_printf swprintf
# else
# define sys_printf snprintf
# endif
// ----------------------------------------------------------------------------
// main
// ----------------------------------------------------------------------------
2009-05-30 09:54:22 -04:00
BENCHMARK_FUNC ( SnprintfWithPositionals )
2006-09-05 17:14:38 -04:00
{
wxChar buffer [ BUFSIZE ] ;
# if wxUSE_PRINTF_POS_PARAMS
2009-05-30 09:54:22 -04:00
DO_LONG_POSITIONAL_BENCHMARK ( wxSnprintf , )
DO_POSITIONAL_BENCHMARK ( wxSnprintf , )
2006-09-05 17:14:38 -04:00
# endif
2009-05-30 09:54:22 -04:00
return true ;
}
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
BENCHMARK_FUNC ( Snprintf )
{
wxChar buffer [ BUFSIZE ] ;
DO_LONG_BENCHMARK ( wxSnprintf , )
DO_BENCHMARK ( wxSnprintf , )
return true ;
}
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
BENCHMARK_FUNC ( SystemSnprintfWithPositionals )
{
wxChar buffer [ BUFSIZE ] ;
DO_LONG_POSITIONAL_BENCHMARK ( sys_printf , L )
DO_POSITIONAL_BENCHMARK ( sys_printf , L )
return true ;
}
2006-09-05 17:14:38 -04:00
2009-05-30 09:54:22 -04:00
BENCHMARK_FUNC ( SystemSnprintf )
{
wxChar buffer [ BUFSIZE ] ;
DO_LONG_BENCHMARK ( sys_printf , L )
DO_BENCHMARK ( sys_printf , L )
return true ;
2006-09-05 17:14:38 -04:00
}