2
0
mirror of https://github.com/boostorg/math.git synced 2026-02-17 01:42:15 +00:00
Files
math/performance/performance_measure.hpp
John Maddock 5e9204816c Added short section on performance test app.
Added copyright info to performance test app, plus needed build macros.

[SVN r39058]
2007-08-29 18:30:05 +00:00

87 lines
1.9 KiB
C++

// Copyright John Maddock 2007.
// Use, modification and distribution are subject to 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)
#ifndef BOOST_MATH_PERFORMANCE_MEASURE_HPP
#define BOOST_MATH_PERFORMANCE_MEASURE_HPP
#include <boost/config.hpp>
#include <boost/timer.hpp>
#include <cstring>
template <class F>
double performance_measure(F f)
{
unsigned count = 1;
double time, result;
//
// Begin by figuring out how many times to repeat
// the function call in order to get a measureable time:
//
do
{
boost::timer t;
for(unsigned i = 0; i < count; ++i)
f();
time = t.elapsed();
count *= 2;
t.restart();
}while(time < 0.5);
count /= 2;
result = time;
//
// Now repeat the measurement over and over
// and take the shortest measured time as the
// result, generally speaking this gives us
// consistent results:
//
for(unsigned i = 0; i < 10u;++i)
{
boost::timer t;
for(unsigned i = 0; i < count; ++i)
f();
time = t.elapsed();
if(time < result)
result = time;
t.restart();
}
return result / count;
}
struct test_info
{
test_info(const char* n, void (*p)());
const char* name;
void (*proc)();
};
inline bool operator < (const test_info& a, const test_info& b)
{
return std::strcmp(a.name, b.name) < 0;
}
extern void consume_result(double);
extern void set_call_count(int i);
extern void add_new_test(test_info i);
inline test_info::test_info(const char* n, void (*p)())
{
name = n;
proc = p;
add_new_test(*this);
}
#define BOOST_MATH_PERFORMANCE_TEST(name, string) \
void BOOST_JOIN(name, __LINE__) ();\
namespace{\
test_info BOOST_JOIN(BOOST_JOIN(name, _init), __LINE__)(string, BOOST_JOIN(name, __LINE__));\
}\
void BOOST_JOIN(name, __LINE__) ()
#endif // BOOST_MATH_PERFORMANCE_MEASURE_HPP