format_performance.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2002-2010 Hartmut Kaiser
  2. // Copyright (c) 2002-2010 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config/warning_disable.hpp>
  7. #include <boost/spirit/include/karma.hpp>
  8. #include <boost/format.hpp>
  9. #include <iostream>
  10. #include "../high_resolution_timer.hpp"
  11. #define NUMITERATIONS 1000000
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // policy for real_generator, which forces to output trailing zeros in the
  14. // fractional part
  15. //[karma_format_performance_definitions
  16. template <typename T>
  17. struct double3_policy : boost::spirit::karma::real_policies<T>
  18. {
  19. // we want to generate up to 3 fractional digits
  20. static unsigned int precision(T) { return 3; }
  21. };
  22. typedef boost::spirit::karma::real_generator<double, double3_policy<double> >
  23. double3_type;
  24. double3_type const double3 = double3_type();
  25. //]
  26. void format_performance_karma()
  27. {
  28. using boost::spirit::karma::left_align;
  29. using boost::spirit::karma::generate;
  30. //[karma_format_performance_plain
  31. char buffer[256];
  32. //<-
  33. util::high_resolution_timer t;
  34. //->
  35. for (int i = 0; i < NUMITERATIONS; ++i) {
  36. char *p = buffer;
  37. generate(p
  38. , '[' << left_align(14)[double3] << left_align(14)[double3] << ']'
  39. , 12345.12345, 12345.12345);
  40. *p = '\0';
  41. }
  42. //]
  43. std::cout << "karma:\t\t" << t.elapsed() << std::endl;
  44. // std::cout << buffer << std::endl;
  45. }
  46. void format_performance_rule()
  47. {
  48. using boost::spirit::karma::left_align;
  49. using boost::spirit::karma::generate;
  50. typedef boost::fusion::vector<double, double> rtype;
  51. boost::spirit::karma::rule<char*, rtype()> r;
  52. //[karma_format_performance_rule
  53. char buffer[256];
  54. r %= '[' << left_align(14)[double3] << left_align(14)[double3] << ']';
  55. //<-
  56. util::high_resolution_timer t;
  57. //->
  58. for (int i = 0; i < NUMITERATIONS; ++i) {
  59. char *p = buffer;
  60. generate(p, r, 12345.12345, 12345.12345);
  61. *p = '\0';
  62. }
  63. //]
  64. std::cout << "karma (rule):\t" << t.elapsed() << std::endl;
  65. // std::cout << buffer << std::endl;
  66. }
  67. void format_performance_string()
  68. {
  69. using boost::spirit::karma::left_align;
  70. using boost::spirit::karma::generate;
  71. //[karma_format_performance_string
  72. std::string generated;
  73. std::back_insert_iterator<std::string> sink(generated);
  74. //<-
  75. util::high_resolution_timer t;
  76. //->
  77. for (int i = 0; i < NUMITERATIONS; ++i) {
  78. generated.clear();
  79. generate(sink
  80. , '[' << left_align(14)[double3] << left_align(14)[double3] << ']'
  81. , 12345.12345, 12345.12345);
  82. }
  83. //]
  84. std::cout << "karma (string):\t" << t.elapsed() << std::endl;
  85. // std::cout << generated << std::endl;
  86. }
  87. // Boost.Format
  88. void format_performance_boost_format()
  89. {
  90. //[karma_format_performance_format
  91. std::string generated;
  92. boost::format outformat("[%-14.3f%-14.3f]");
  93. //<-
  94. util::high_resolution_timer t;
  95. //->
  96. for (int i = 0; i < NUMITERATIONS; ++i)
  97. generated = boost::str(outformat % 12345.12345 % 12345.12345);
  98. //]
  99. std::cout << "format:\t\t" << t.elapsed() << std::endl;
  100. // std::cout << strm.str() << std::endl;
  101. }
  102. void format_performance_sprintf()
  103. {
  104. util::high_resolution_timer t;
  105. //[karma_format_performance_printf
  106. char buffer[256];
  107. for (int i = 0; i < NUMITERATIONS; ++i) {
  108. sprintf(buffer, "[%-14.3f%-14.3f]", 12345.12345, 12345.12345);
  109. }
  110. //]
  111. std::cout << "sprintf:\t" << t.elapsed() << std::endl;
  112. // std::cout << buffer << std::endl;
  113. }
  114. void format_performance_iostreams()
  115. {
  116. //[karma_format_performance_iostreams
  117. std::stringstream strm;
  118. //<-
  119. util::high_resolution_timer t;
  120. //->
  121. for (int i = 0; i < NUMITERATIONS; ++i) {
  122. strm.str("");
  123. strm << '['
  124. << std::setiosflags(std::ios::fixed)
  125. << std::left
  126. << std::setprecision(3)
  127. << std::setw(14)
  128. << 12345.12345
  129. << std::setw(14)
  130. << 12345.12345
  131. << ']';
  132. }
  133. //]
  134. std::cout << "iostreams:\t" << t.elapsed() << std::endl;
  135. // std::cout << strm.str() << std::endl;
  136. }
  137. ///////////////////////////////////////////////////////////////////////////////
  138. int main()
  139. {
  140. format_performance_sprintf();
  141. format_performance_iostreams();
  142. format_performance_boost_format();
  143. format_performance_karma();
  144. format_performance_string();
  145. format_performance_rule();
  146. return 0;
  147. }