// // Copyright Edward J. Grace 2008 - 2009. // 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) // //#define _GLIBCXX_DEBUG 1 // // CVS Information // // $Author: graceej $ $Date: 2009/07/27 10:28:57 $ // $Revision: 1.1.2.3 $ // #include #include "cycle.h" #include #include // // Let us answer some silly questions. // // // Q) Which is faster division by x or multiplication by 1.0/x? // // A) Unsurprisingly it depends on the numbers! // inline double multiply(const double &x, const double &one_over_y) { return x*one_over_y; } inline double divide(const double &x, const double &y) { return x/y; } // // First extend the generic timer and add a few member functions as a // convenience. // class timer : protected ejg::generic_timer { double min_percent,med_percent,max_percent; public: timer() : min_percent(0), med_percent(0), max_percent(0), ejg::generic_timer(getticks,elapsed) { // During construction calibrate the chronometer. set_nominal_precision_target_percent(1.0); calibrate_chrono_overhead(); } public: template void test(O a, O b) { measure_percentage_speedup(a,b, min_percent, med_percent, max_percent); } bool was_ok() const { return ( min_percent == min_percent && max_percent == max_percent) && !(min_percent <= 0.0 && max_percent >= 0.0); } bool was_faster() const { return (min_percent > 0.0); } bool was_slower() const { return (max_percent < 0.0); } double get_last_min() const { return min_percent; } double get_last_med() const { return med_percent; } double get_last_max() const { return max_percent; } }; int main(void) { timer t; std::cout << "Enter a number: "; double a; std::cin >> a; std::cout << "Enter another: "; double b; std::cin >> b; double one_over_b(1.0/b); // Using boost::bind, time the two options. t.test(boost::bind(multiply, a, one_over_b), boost::bind(divide, a, b)); // Produce a report regarding the timed values. We are just // reporting the central (median) point estimate of the speedup. // The speedup that we measure can be in a large bracket // (e.g. 110%-195%), consequently the reported point estimate may // apparently vary from run-to-run. Looking at the confidence // interval you may note that this apparent variation is not // statistically significant. std::cout << "\n\n"; std::cout << "When evaluating " << a << "*" << one_over_b << " vs. " << a << "/" << b << std::endl; if (t.was_ok()) if (t.was_faster()) std::cout << "Multiplication is quicker than division by around: " << t.get_last_med() << "%\n"; else std::cout << "Division is quicker than multiplication by around: " << (100.0-t.get_last_med()) << "%\n"; else std::cout << "There is no practial difference between division and " << "multiplication\n"; std::cout << std::endl; // Spit out the difference between the two. double error(divide(a,b)-multiply(a,one_over_b)); std::cout << "The difference is : " << error << "\n\n"; return 0; } // // $Log: example_binding.cpp,v $ // Revision 1.1.2.3 2009/07/27 10:28:57 graceej // * Compiled with checked bounds. // * Added comments relating to the reported confidence interval. // // Revision 1.1.2.2 2009/07/27 10:15:48 graceej // * Reformatted. // // Revision 1.1.2.1 2009/07/27 10:10:59 graceej // * Example using boost::bind to test two very fast functions. // //