real_generator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <cassert>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <boost/format.hpp>
  10. #include <boost/spirit/include/karma.hpp>
  11. #include "../high_resolution_timer.hpp"
  12. using namespace std;
  13. using namespace boost::spirit;
  14. #define MAX_ITERATION 10000000
  15. ///////////////////////////////////////////////////////////////////////////////
  16. struct random_fill
  17. {
  18. double operator()() const
  19. {
  20. double scale = std::rand() / 100 + 1;
  21. return double(std::rand() * std::rand()) / scale;
  22. }
  23. };
  24. ///////////////////////////////////////////////////////////////////////////////
  25. int main()
  26. {
  27. namespace karma = boost::spirit::karma;
  28. char buffer[512]; // we don't expect more than 512 bytes to be generated
  29. cout << "Converting " << MAX_ITERATION
  30. << " randomly generated double values to strings." << flush << endl;
  31. std::srand(0);
  32. std::vector<double> v (MAX_ITERATION);
  33. std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector
  34. // test the C libraries gcvt function (the most low level function for
  35. // string conversion available)
  36. {
  37. std::string str;
  38. util::high_resolution_timer t;
  39. for (int i = 0; i < MAX_ITERATION; ++i)
  40. {
  41. gcvt(v[i], 10, buffer);
  42. str = buffer; // compensate for string ops in other benchmarks
  43. }
  44. cout << "gcvt: " << t.elapsed() << " [s]" << flush << endl;
  45. }
  46. // test the iostreams library
  47. {
  48. std::stringstream str;
  49. util::high_resolution_timer t;
  50. for (int i = 0; i < MAX_ITERATION; ++i)
  51. {
  52. str.str("");
  53. str << v[i];
  54. }
  55. cout << "iostreams: " << t.elapsed() << " [s]" << flush << endl;
  56. }
  57. // test the Boost.Format library
  58. {
  59. std::string str;
  60. boost::format double_format("%f");
  61. util::high_resolution_timer t;
  62. for (int i = 0; i < MAX_ITERATION; ++i)
  63. {
  64. str = boost::str(double_format % v[i]);
  65. }
  66. cout << "Boost.Format: " << t.elapsed() << " [s]" << flush << endl;
  67. }
  68. // test the Karma double_ generation routines
  69. {
  70. std::string str;
  71. util::high_resolution_timer t;
  72. for (int i = 0; i < MAX_ITERATION; ++i)
  73. {
  74. char *ptr = buffer;
  75. karma::generate(ptr, double_, v[i]);
  76. *ptr = '\0';
  77. str = buffer; // compensate for string ops in other benchmarks
  78. }
  79. cout << "double_: " << t.elapsed() << " [s]" << flush << endl;
  80. }
  81. return 0;
  82. }