printfbench.cpp 6.0 KB

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