mirror of
https://github.com/boostorg/endian.git
synced 2025-08-02 05:54:31 +02:00
Further benchmark improvements
git-svn-id: http://svn.boost.org/svn/boost/sandbox/endian@74297 b8fc166d-592f-0410-95f2-cb63ce0dd405
This commit is contained in:
@@ -7,11 +7,13 @@
|
|||||||
|
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <boost/endian/conversion.hpp>
|
#include <boost/endian/conversion.hpp>
|
||||||
#include <boost/random.hpp>
|
#include <boost/random.hpp>
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
#include <boost/endian/support/timer.hpp>
|
#include <boost/endian/support/timer.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
@@ -19,33 +21,44 @@ using std::cerr;
|
|||||||
using std::endl;
|
using std::endl;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
#define BENCHMARK(Function) \
|
|
||||||
{ \
|
|
||||||
cout << "\nRunning benchmark..." << endl << ' '; \
|
|
||||||
int64_t sum = 0; \
|
|
||||||
int32_t value; \
|
|
||||||
\
|
|
||||||
endian::run_timer t; \
|
|
||||||
\
|
|
||||||
for (int32_t i = n; i; --i) \
|
|
||||||
{ \
|
|
||||||
value = 0x01020304; \
|
|
||||||
Function(value); \
|
|
||||||
sum += value ; \
|
|
||||||
} \
|
|
||||||
\
|
|
||||||
t.report(); \
|
|
||||||
\
|
|
||||||
cout << " Benchmark complete\n" \
|
|
||||||
" sum is " << sum << endl; \
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
std::string command_args;
|
std::string command_args;
|
||||||
long n;
|
long long n;
|
||||||
long seed = 1;
|
|
||||||
int places = 2;
|
int places = 2;
|
||||||
|
bool verbose (false);
|
||||||
|
|
||||||
|
typedef int32_t (*timee_func)(int32_t);
|
||||||
|
|
||||||
|
endian::microsecond_t benchmark(timee_func timee, const char* msg,
|
||||||
|
endian::microsecond_t overhead = 0)
|
||||||
|
{
|
||||||
|
if (verbose)
|
||||||
|
cout << "\nRunning benchmark..." << endl;
|
||||||
|
int64_t sum = 0;
|
||||||
|
endian::times_t times;
|
||||||
|
endian::microsecond_t cpu_time;
|
||||||
|
endian::run_timer t(places);
|
||||||
|
|
||||||
|
for (long long i = n; i; --i)
|
||||||
|
{
|
||||||
|
sum += timee(static_cast<int32_t>(i)) ;
|
||||||
|
}
|
||||||
|
times = t.stop();
|
||||||
|
cpu_time = (times.system + times.user) - overhead;
|
||||||
|
const long double sec = 1000000.0L;
|
||||||
|
cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
|
||||||
|
cout.precision(places);
|
||||||
|
cout << msg << " " << cpu_time / sec << endl;
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
t.report();
|
||||||
|
cout << " Benchmark complete\n"
|
||||||
|
" sum is " << sum << endl;
|
||||||
|
}
|
||||||
|
return cpu_time;
|
||||||
|
}
|
||||||
|
|
||||||
void process_command_line(int argc, char * argv[])
|
void process_command_line(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
@@ -59,12 +72,18 @@ namespace
|
|||||||
cout << command_args << '\n';;
|
cout << command_args << '\n';;
|
||||||
|
|
||||||
if (argc >=2)
|
if (argc >=2)
|
||||||
n = std::atol(argv[1]);
|
#ifndef _MSC_VER
|
||||||
|
n = std::atoll(argv[1]);
|
||||||
|
#else
|
||||||
|
n = _atoi64(argv[1]);
|
||||||
|
#endif
|
||||||
|
|
||||||
for (; argc > 2; ++argv, --argc)
|
for (; argc > 2; ++argv, --argc)
|
||||||
{
|
{
|
||||||
if ( *(argv[2]+1) == 'p' )
|
if ( *(argv[2]+1) == 'p' )
|
||||||
places = atoi( argv[2]+2 );
|
places = atoi( argv[2]+2 );
|
||||||
|
else if ( *(argv[2]+1) == 'v' )
|
||||||
|
verbose = true;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "Error - unknown option: " << argv[2] << "\n\n";
|
cout << "Error - unknown option: " << argv[2] << "\n\n";
|
||||||
@@ -78,19 +97,43 @@ namespace
|
|||||||
cout << "Usage: benchmark n [Options]\n"
|
cout << "Usage: benchmark n [Options]\n"
|
||||||
" The argument n specifies the number of test cases to run\n"
|
" The argument n specifies the number of test cases to run\n"
|
||||||
" Options:\n"
|
" Options:\n"
|
||||||
|
" -v Verbose messages\n"
|
||||||
" -p# Decimal places for times; default -p" << places << "\n";
|
" -p# Decimal places for times; default -p" << places << "\n";
|
||||||
return std::exit(1);
|
return std::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void noop(int32_t&) {}
|
inline void in_place(int32_t& x)
|
||||||
|
|
||||||
inline void shift_and_mask(int32_t& x)
|
|
||||||
{
|
{
|
||||||
x = ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | ((x >> 24) & 0x000000ff)
|
x = ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | ((x >> 24) & 0x000000ff)
|
||||||
| ((x >> 8) & 0x0000ff00);
|
| ((x >> 8) & 0x0000ff00);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int32_t by_return(int32_t x)
|
||||||
|
{
|
||||||
|
return ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | ((x >> 24) & 0x000000ff)
|
||||||
|
| ((x >> 8) & 0x0000ff00);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t modify_noop(int32_t x)
|
||||||
|
{
|
||||||
|
int32_t v(x);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t modify_in_place(int32_t x)
|
||||||
|
{
|
||||||
|
int32_t v(x);
|
||||||
|
in_place(v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t modify_by_return(int32_t x)
|
||||||
|
{
|
||||||
|
int32_t v(x);
|
||||||
|
return by_return(v);
|
||||||
|
}
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
//-------------------------------------- main() ---------------------------------------//
|
//-------------------------------------- main() ---------------------------------------//
|
||||||
@@ -99,9 +142,11 @@ int main(int argc, char * argv[])
|
|||||||
{
|
{
|
||||||
process_command_line(argc, argv);
|
process_command_line(argc, argv);
|
||||||
|
|
||||||
BENCHMARK(noop);
|
endian::microsecond_t overhead;
|
||||||
BENCHMARK(endian::reorder);
|
|
||||||
BENCHMARK(shift_and_mask);
|
overhead = benchmark(modify_noop, "modify no-op");
|
||||||
|
benchmark(modify_in_place, "modify in place", overhead);
|
||||||
|
benchmark(modify_by_return, "modify by return", overhead);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user