File: printfbench.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (139 lines) | stat: -rw-r--r-- 6,179 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/////////////////////////////////////////////////////////////////////////////
// Name:        printfbench.cpp
// Purpose:     benchmarks for wx*Printf*() functions
// Author:      Francesco Montorsi
// Modified by:
// Created:     27/3/2006
// Copyright:   (c) 2006-2009 Francesco Montorsi
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/*
    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).
*/

//
// 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"
//

#include "wx/string.h"
#include "bench.h"

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

#define BUFSIZE                  10000

const wxString g_verylongString =
    "very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very "
    "very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very 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";


// ----------------------------------------------------------------------------
// benchmarking helpers
// ----------------------------------------------------------------------------

#define DO_LONG_BENCHMARK(fnc, prefix)                                                     \
    fnc(buffer, BUFSIZE,                                                                   \
        prefix##"This is a reasonably long string with various %s arguments, exactly %d, " \
        prefix##"and is used as benchmark for %s - %% %.2f %d %s",                                 \
        prefix##"(many!!)", 6, "this program", 23.342f, 999,                                       \
        (const char*)g_verylongString.c_str());

#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 "                \
        prefix##"for our benchmarking aims - %% %3$f %5$d %4$s",                                   \
        prefix##"test it", "string with positional arguments", 23.342f,                            \
        (const char*)g_verylongString.c_str(), 999);

#define DO_BENCHMARK(fnc, prefix)                                                          \
    fnc(buffer, BUFSIZE, prefix##"This is a short %s string with very few words", "test");

#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");

// 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
// ----------------------------------------------------------------------------

BENCHMARK_FUNC(SnprintfWithPositionals)
{
    wxChar buffer[BUFSIZE];
#if wxUSE_PRINTF_POS_PARAMS
    DO_LONG_POSITIONAL_BENCHMARK(wxSnprintf, )
    DO_POSITIONAL_BENCHMARK(wxSnprintf, )
#endif
    return true;
}

BENCHMARK_FUNC(Snprintf)
{
    wxChar buffer[BUFSIZE];
    DO_LONG_BENCHMARK(wxSnprintf, )
    DO_BENCHMARK(wxSnprintf, )
    return true;
}

BENCHMARK_FUNC(SystemSnprintfWithPositionals)
{
    wxChar buffer[BUFSIZE];
    DO_LONG_POSITIONAL_BENCHMARK(sys_printf, L)
    DO_POSITIONAL_BENCHMARK(sys_printf, L)
    return true;
}

BENCHMARK_FUNC(SystemSnprintf)
{
    wxChar buffer[BUFSIZE];
    DO_LONG_BENCHMARK(sys_printf, L)
    DO_BENCHMARK(sys_printf, L)
    return true;
}