1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printfbench.cpp
3 // Purpose: benchmarks for wx*Printf*() functions
4 // Author: Francesco Montorsi
8 // Copyright: (c) 2006-2009 Francesco Montorsi
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 TODO: revise the benchmarking functions below to allow valid comparisons
14 between the wx implementation and the system's implementation of
15 the tested functions (e.g. adding tests which use the wxS macro to
16 avoid runtime encoding conversions, etc etc).
20 // Profiling under Linux:
21 // =====================
23 // 1) configure wxWidgets in release mode
24 // 2) make sure that HAVE_UNIX98_PRINTF is undefined (just #defining it to zero
25 // does not work; you must comment out the entire #define) in your setup.h;
26 // and also that wxUSE_PRINTF_POS_PARAMS is set to 1; this will force the
27 // use of wx's own implementation of wxVsnprintf()
29 // 4) set wxTEST_WX_ONLY to 1 and compile tests as well
31 // Now you have two main choices:
34 // 5) add to the Makefile of this test program the -pg option both to
35 // CXXFLAGS and to LDFLAGS
37 // 7) look at the gmon.out file with gprof utility
40 // 4) run "valgrind --tool=callgrind ./printfbench"
41 // 5) run "kcachegrind dump_file_generated_by_callgrind"
44 #include "wx/string.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
53 const wxString g_verylongString
=
54 "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 "
55 "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";
58 // ----------------------------------------------------------------------------
59 // benchmarking helpers
60 // ----------------------------------------------------------------------------
62 #define DO_LONG_BENCHMARK(fnc, prefix) \
63 fnc(buffer, BUFSIZE, \
64 prefix##"This is a reasonably long string with various %s arguments, exactly %d, " \
65 prefix##"and is used as benchmark for %s - %% %.2f %d %s", \
66 prefix##"(many!!)", 6, "this program", 23.342f, 999, \
67 (const char*)g_verylongString.c_str());
69 #define DO_LONG_POSITIONAL_BENCHMARK(fnc, prefix) \
70 fnc(buffer, BUFSIZE, \
71 prefix##"This is a %2$s and thus is harder to parse... let's %1$s " \
72 prefix##"for our benchmarking aims - %% %3$f %5$d %4$s", \
73 prefix##"test it", "string with positional arguments", 23.342f, \
74 (const char*)g_verylongString.c_str(), 999);
76 #define DO_BENCHMARK(fnc, prefix) \
77 fnc(buffer, BUFSIZE, prefix##"This is a short %s string with very few words", "test");
79 #define DO_POSITIONAL_BENCHMARK(fnc, prefix) \
80 fnc(buffer, BUFSIZE, \
81 prefix##"This is a %2$s and thus is harder to parse... nonetheless, %1$s !", \
82 "test it", "string with positional arguments");
84 // the configure script of wxWidgets will define HAVE_UNIX98_PRINTF on those
85 // system with a *printf() family of functions conformant to Unix 98 standard;
86 // systems without the configure script as build system (e.g. Windows) do not
87 // have positional support anyway
88 #ifdef HAVE_UNIX98_PRINTF
89 #define wxSYSTEM_HAS_POSPARAM_SUPPORT 1
91 #define wxSYSTEM_HAS_POSPARAM_SUPPORT 1
94 // we need to avoid the use of wxPrintf() here since it could have been mapped
95 // to wxWidgets' implementation of wxVsnPrintf() !
97 #define sys_printf swprintf
99 #define sys_printf snprintf
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 BENCHMARK_FUNC(SnprintfWithPositionals
)
109 wxChar buffer
[BUFSIZE
];
110 #if wxUSE_PRINTF_POS_PARAMS
111 DO_LONG_POSITIONAL_BENCHMARK(wxSnprintf
, )
112 DO_POSITIONAL_BENCHMARK(wxSnprintf
, )
117 BENCHMARK_FUNC(Snprintf
)
119 wxChar buffer
[BUFSIZE
];
120 DO_LONG_BENCHMARK(wxSnprintf
, )
121 DO_BENCHMARK(wxSnprintf
, )
125 BENCHMARK_FUNC(SystemSnprintfWithPositionals
)
127 wxChar buffer
[BUFSIZE
];
128 DO_LONG_POSITIONAL_BENCHMARK(sys_printf
, L
)
129 DO_POSITIONAL_BENCHMARK(sys_printf
, L
)
133 BENCHMARK_FUNC(SystemSnprintf
)
135 wxChar buffer
[BUFSIZE
];
136 DO_LONG_BENCHMARK(sys_printf
, L
)
137 DO_BENCHMARK(sys_printf
, L
)