Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r55142 - trunk/libs/spirit/benchmarks/qi
From: joel_at_[hidden]
Date: 2009-07-24 00:10:16


Author: djowel
Date: 2009-07-24 00:10:16 EDT (Fri, 24 Jul 2009)
New Revision: 55142
URL: http://svn.boost.org/trac/boost/changeset/55142

Log:
double number parse benchmarks
Added:
   trunk/libs/spirit/benchmarks/qi/real_parser.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/benchmarks/qi/int_parser.cpp | 6 ++----
   1 files changed, 2 insertions(+), 4 deletions(-)

Modified: trunk/libs/spirit/benchmarks/qi/int_parser.cpp
==============================================================================
--- trunk/libs/spirit/benchmarks/qi/int_parser.cpp (original)
+++ trunk/libs/spirit/benchmarks/qi/int_parser.cpp 2009-07-24 00:10:16 EDT (Fri, 24 Jul 2009)
@@ -64,10 +64,7 @@
         }
 
         void benchmark()
- {
- namespace qi = boost::spirit::qi;
- using qi::int_;
-
+ {
             for (int i = 0; i < 9; ++i)
                 this->val += parse(first[i], last[i]);
         }
@@ -76,6 +73,7 @@
 
 int main()
 {
+ // Seed the random generator
     srand(time(0));
     
     // Generate random integers with 1 .. 9 digits

Added: trunk/libs/spirit/benchmarks/qi/real_parser.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/benchmarks/qi/real_parser.cpp 2009-07-24 00:10:16 EDT (Fri, 24 Jul 2009)
@@ -0,0 +1,107 @@
+/*=============================================================================
+ Copyright (c) 2001-2009 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+==============================================================================*/
+#include "../measure.hpp"
+#include <string>
+#include <vector>
+#include <cstdlib>
+#include <boost/spirit/include/qi.hpp>
+
+namespace
+{
+ int const ndigits = 9;
+ std::string numbers[ndigits] =
+ {
+ "1234",
+ "-1.2e3",
+ "0.1",
+ "-1.2e-3",
+ "-.2e3",
+ "-e3",
+ "1.2e",
+ "-5.7222349715140557e+307",
+ "2.0332938517515416e-308"
+ };
+
+ char const* first[ndigits];
+ char const* last[ndigits];
+
+ ///////////////////////////////////////////////////////////////////////////
+ struct atof_test : test::base
+ {
+ void benchmark()
+ {
+ for (int i = 0; i < ndigits; ++i)
+ {
+ double d = atof(first[i]);
+ this->val += *reinterpret_cast<int*>(&d);
+ }
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ struct strtod_test : test::base
+ {
+ void benchmark()
+ {
+ for (int i = 0; i < ndigits; ++i)
+ {
+ double d = strtod(first[i], const_cast<char**>(&last[i]));
+ this->val += *reinterpret_cast<int*>(&d);
+ }
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ struct spirit_double_test : test::base
+ {
+ static double parse(char const* first, char const* last)
+ {
+ double n;
+ namespace qi = boost::spirit::qi;
+ using qi::double_;
+ qi::parse(first, last, double_, n);
+ return n;
+ }
+
+ void benchmark()
+ {
+ for (int i = 0; i < ndigits; ++i)
+ {
+ double d = parse(first[i], last[i]);
+ this->val += *reinterpret_cast<int*>(&d);
+ }
+ }
+ };
+}
+
+int main()
+{
+ std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
+ std::cout << "Numbers to test:" << std::endl;
+ for (int i = 0; i < ndigits; ++i)
+ {
+ first[i] = numbers[i].c_str();
+ last[i] = first[i];
+ while (*last[i])
+ last[i]++;
+ std::cout << numbers[i] << std::endl;
+ }
+ std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
+
+ BOOST_SPIRIT_TEST_BENCHMARK(
+ 10000000, // This is the maximum repetitions to execute
+ (atof_test)
+ (strtod_test)
+ (spirit_double_test)
+ )
+
+ // This is ultimately responsible for preventing all the test code
+ // from being optimized away. Change this to return 0 and you
+ // unplug the whole test's life support system.
+ return test::live_code != 0;
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk