int_generator.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <climits>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <boost/format.hpp>
  10. #include "../high_resolution_timer.hpp"
  11. // This value specifies, how to unroll the integer string generation loop in
  12. // Karma.
  13. // Set this to some integer in between 0 (no unrolling) and max expected
  14. // integer string len (complete unrolling). If not specified, this value
  15. // defaults to 6.
  16. #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
  17. #include <boost/spirit/include/karma.hpp>
  18. using namespace std;
  19. using namespace boost::spirit;
  20. #define MAX_ITERATION 10000000
  21. ///////////////////////////////////////////////////////////////////////////////
  22. struct random_fill
  23. {
  24. int operator()() const
  25. {
  26. int scale = std::rand() / 100 + 1;
  27. return (std::rand() * std::rand()) / scale;
  28. }
  29. };
  30. ///////////////////////////////////////////////////////////////////////////////
  31. int main()
  32. {
  33. namespace karma = boost::spirit::karma;
  34. cout << "Converting " << MAX_ITERATION
  35. << " randomly generated int values to strings." << flush << endl;
  36. std::srand(0);
  37. std::vector<int> v (MAX_ITERATION);
  38. std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector
  39. // test the C libraries ltoa function (the most low level function for
  40. // string conversion available)
  41. {
  42. //[karma_int_performance_ltoa
  43. char buffer[65]; // we don't expect more than 64 bytes to be generated here
  44. //<-
  45. std::string str;
  46. util::high_resolution_timer t;
  47. //->
  48. for (int i = 0; i < MAX_ITERATION; ++i)
  49. {
  50. ltoa(v[i], buffer, 10);
  51. //<-
  52. str = buffer; // compensate for string ops in other benchmarks
  53. //->
  54. }
  55. //]
  56. cout << "ltoa:\t\t" << t.elapsed() << " [s]" << flush << endl;
  57. }
  58. // test the iostreams library
  59. {
  60. //[karma_int_performance_iostreams
  61. std::stringstream str;
  62. //<-
  63. util::high_resolution_timer t;
  64. //->
  65. for (int i = 0; i < MAX_ITERATION; ++i)
  66. {
  67. str.str("");
  68. str << v[i];
  69. }
  70. //]
  71. cout << "iostreams:\t" << t.elapsed() << " [s]" << flush << endl;
  72. }
  73. // test the Boost.Format library
  74. {
  75. //[karma_int_performance_format
  76. std::string str;
  77. boost::format int_format("%d");
  78. //<-
  79. util::high_resolution_timer t;
  80. //->
  81. for (int i = 0; i < MAX_ITERATION; ++i)
  82. {
  83. str = boost::str(int_format % v[i]);
  84. }
  85. //]
  86. cout << "Boost.Format:\t" << t.elapsed() << " [s]" << flush << endl;
  87. }
  88. // test the Karma int_ generation routines
  89. {
  90. std::string str;
  91. util::high_resolution_timer t;
  92. //[karma_int_performance_plain
  93. char buffer[65]; // we don't expect more than 64 bytes to be generated here
  94. for (int i = 0; i < MAX_ITERATION; ++i)
  95. {
  96. char *ptr = buffer;
  97. karma::generate(ptr, int_, v[i]);
  98. *ptr = '\0';
  99. //<-
  100. str = buffer; // compensate for string ops in other benchmarks
  101. //->
  102. }
  103. //]
  104. cout << "int_:\t\t" << t.elapsed() << " [s]" << flush << endl;
  105. }
  106. return 0;
  107. }