Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r55087 - in trunk/libs/spirit/benchmarks: . qi
From: joel_at_[hidden]
Date: 2009-07-22 14:24:37


Author: djowel
Date: 2009-07-22 14:24:36 EDT (Wed, 22 Jul 2009)
New Revision: 55087
URL: http://svn.boost.org/trac/boost/changeset/55087

Log:
updated int_ benchmarks to use the test harness
Text files modified:
   trunk/libs/spirit/benchmarks/boiler_plate.cpp | 2
   trunk/libs/spirit/benchmarks/measure.hpp | 10 +-
   trunk/libs/spirit/benchmarks/qi/uint_parser.cpp | 128 +++++++++++++++++++++++++++++++++------
   3 files changed, 113 insertions(+), 27 deletions(-)

Modified: trunk/libs/spirit/benchmarks/boiler_plate.cpp
==============================================================================
--- trunk/libs/spirit/benchmarks/boiler_plate.cpp (original)
+++ trunk/libs/spirit/benchmarks/boiler_plate.cpp 2009-07-22 14:24:36 EDT (Wed, 22 Jul 2009)
@@ -24,7 +24,7 @@
 int main()
 {
     BOOST_SPIRIT_TEST_BENCHMARK(
- 100000, // This is the maximum repetitions to execute
+ 10000000, // This is the maximum repetitions to execute
         (f) // Place your tests here. For now, we have only one test: (f)
                     // If you have 3 tests a, b and c, this line will contain (a)(b)(c)
     )

Modified: trunk/libs/spirit/benchmarks/measure.hpp
==============================================================================
--- trunk/libs/spirit/benchmarks/measure.hpp (original)
+++ trunk/libs/spirit/benchmarks/measure.hpp 2009-07-22 14:24:36 EDT (Wed, 22 Jul 2009)
@@ -14,8 +14,8 @@
 
 #include "high_resolution_timer.hpp"
 #include <iostream>
-#include <boost/preprocessor/seq/for_each.hpp>
-#include <boost/preprocessor/stringize.hpp>
+#include <boost/preprocessor/seq/for_each.hpp>
+#include <boost/preprocessor/stringize.hpp>
 
 namespace test
 {
@@ -68,7 +68,7 @@
 
         // Accumulate all the partial sums to avoid dead code
         // elimination.
- for (Accumulator* ap = a; ap < a + number_of_accumulators; ++ap)
+ for (Accumulator* ap = a; ap < a + number_of_accumulators; ++ap)
         {
             live_code += ap->val;
         }
@@ -98,10 +98,12 @@
     /***/
 
 #define BOOST_SPIRIT_TEST_MEASURE(r, data, elem) \
+ std::cout.precision(10); \
     std::cout \
         << BOOST_PP_STRINGIZE(elem) << ": " \
+ << std::fixed \
         << test::measure<elem>(repeats) \
- << std::endl;
+ << " [s]" << std::flush << std::endl;
     /***/
 
 #define BOOST_SPIRIT_TEST_BENCHMARK(max_repeats, FSeq) \

Modified: trunk/libs/spirit/benchmarks/qi/uint_parser.cpp
==============================================================================
--- trunk/libs/spirit/benchmarks/qi/uint_parser.cpp (original)
+++ trunk/libs/spirit/benchmarks/qi/uint_parser.cpp 2009-07-22 14:24:36 EDT (Wed, 22 Jul 2009)
@@ -4,39 +4,123 @@
     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 <iostream>
+#include "../measure.hpp"
+#include <string>
+#include <vector>
+#include <cstdlib>
+#include <boost/spirit/include/qi.hpp>
 
 namespace
 {
- struct f
+ // Random number string generator
+ std::string
+ gen_int(int digits)
     {
- typedef int type;
+ std::string result;
+ if (rand()%2) // Prepend a '-'
+ result += '-';
+ result += '1' + (rand()%9); // The first digit cannot be '0'
+
+ for (int i = 1; i < digits; ++i) // Generate the remaining digits
+ result += '0' + (rand()%10);
+ return result;
+ }
+
+ std::string numbers[9];
+ char const* first[9];
+ char const* last[9];
 
- void operator()(int x)
+ ///////////////////////////////////////////////////////////////////////////
+ struct atoi_test
+ {
+ void benchmark(int x)
+ {
+ this->val += atoi(first[0]);
+ this->val += atoi(first[1]);
+ this->val += atoi(first[2]);
+ this->val += atoi(first[3]);
+ this->val += atoi(first[4]);
+ this->val += atoi(first[5]);
+ this->val += atoi(first[6]);
+ this->val += atoi(first[7]);
+ this->val += atoi(first[8]);
+ }
+
+ int val; // This is needed to avoid dead-code elimination
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ struct strtol_test
+ {
+ void benchmark(int x)
+ {
+ this->val += strtol(first[0], const_cast<char**>(&last[0]), 10);
+ this->val += strtol(first[1], const_cast<char**>(&last[1]), 10);
+ this->val += strtol(first[2], const_cast<char**>(&last[2]), 10);
+ this->val += strtol(first[3], const_cast<char**>(&last[3]), 10);
+ this->val += strtol(first[4], const_cast<char**>(&last[4]), 10);
+ this->val += strtol(first[5], const_cast<char**>(&last[5]), 10);
+ this->val += strtol(first[6], const_cast<char**>(&last[6]), 10);
+ this->val += strtol(first[7], const_cast<char**>(&last[7]), 10);
+ this->val += strtol(first[8], const_cast<char**>(&last[8]), 10);
+ }
+
+ int val; // This is needed to avoid dead-code elimination
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ struct spirit_int_test
+ {
+ void benchmark(int x)
         {
- this->val ^= x; // here is where you put code that you want
- // to benchmark. Make sure it returns something.
- // Anything.
+ namespace qi = boost::spirit::qi;
+ using qi::int_;
+ int n;
+
+ qi::parse(first[0], last[0], int_, n); this->val += n;
+ qi::parse(first[1], last[1], int_, n); this->val += n;
+ qi::parse(first[2], last[2], int_, n); this->val += n;
+ qi::parse(first[3], last[3], int_, n); this->val += n;
+ qi::parse(first[4], last[4], int_, n); this->val += n;
+ qi::parse(first[5], last[5], int_, n); this->val += n;
+ qi::parse(first[6], last[6], int_, n); this->val += n;
+ qi::parse(first[7], last[7], int_, n); this->val += n;
+ qi::parse(first[8], last[8], int_, n); this->val += n;
         }
 
- int val; // this is where the value is accumulated
+ int val; // This is needed to avoid dead-code elimination
     };
 }
 
 int main()
 {
- int result;
- double base_time;
- int ret = test::run<f>(result, base_time, 100);
-
- std::cout
- << "f accumulated result: "
- << result
- << std::endl
- << "f time: "
- << base_time
- << std::endl;
-
- return ret;
+ srand(time(0));
+
+ // Generate random integers with 1 .. 9 digits
+ // We test only 9 digits to avoid overflow
+ std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
+ std::cout << "Numbers to test:" << std::endl;
+ for (int i = 0; i < 9; ++i)
+ {
+ numbers[i] = gen_int(i+1);
+ first[i] = numbers[i].c_str();
+ last[i] = first[i];
+ while (*last[i])
+ last[i]++;
+ std::cout << i+1 << " digit number:" << numbers[i] << std::endl;
+ }
+ std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
+
+ BOOST_SPIRIT_TEST_BENCHMARK(
+ 10000000, // This is the maximum repetitions to execute
+ (atoi_test)
+ (strtol_test)
+ (spirit_int_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