mirror of
https://github.com/boostorg/endian.git
synced 2025-08-02 14:04:29 +02:00
Somewhat more interesting results after redoing loop based on iteration count rather than time. Release builds particularly interesting.
This commit is contained in:
@@ -36,8 +36,11 @@ namespace
|
|||||||
|
|
||||||
typedef boost::timer::nanosecond_type nanosecond_t;
|
typedef boost::timer::nanosecond_type nanosecond_t;
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------//
|
||||||
|
|
||||||
nanosecond_t benchmark(timee_func timee, const char* msg,
|
nanosecond_t benchmark(timee_func timee, const char* msg,
|
||||||
nanosecond_t overhead = 0)
|
nanosecond_t overhead = 0)
|
||||||
|
// Returns: total cpu time (i.e. system time + user time)
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
cout << "\nRunning benchmark..." << endl;
|
cout << "\nRunning benchmark..." << endl;
|
||||||
|
@@ -59,6 +59,9 @@
|
|||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
</Link>
|
</Link>
|
||||||
|
<PostBuildEvent>
|
||||||
|
<Command>"$(TargetDir)\$(TargetName).exe" 10000000</Command>
|
||||||
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
<ClCompile>
|
<ClCompile>
|
||||||
@@ -76,6 +79,9 @@
|
|||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
</Link>
|
</Link>
|
||||||
|
<PostBuildEvent>
|
||||||
|
<Command>"$(TargetDir)\$(TargetName).exe" 1000000000</Command>
|
||||||
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\speed_test.cpp" />
|
<ClCompile Include="..\..\speed_test.cpp" />
|
||||||
|
@@ -11,77 +11,164 @@
|
|||||||
|
|
||||||
#include <boost/endian/converters.hpp>
|
#include <boost/endian/converters.hpp>
|
||||||
#include <boost/endian/integers.hpp>
|
#include <boost/endian/integers.hpp>
|
||||||
#include <boost/chrono.hpp>
|
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
|
#include <boost/timer/timer.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <boost/detail/lightweight_main.hpp>
|
#include <boost/detail/lightweight_main.hpp>
|
||||||
|
|
||||||
|
using namespace boost;
|
||||||
using namespace boost::endian;
|
using namespace boost::endian;
|
||||||
using namespace boost::chrono;
|
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using boost::int16_t;
|
|
||||||
using boost::uint16_t;
|
|
||||||
using boost::int32_t;
|
|
||||||
using boost::uint32_t;
|
|
||||||
using boost::int64_t;
|
|
||||||
using boost::uint64_t;
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
long default_length(1);
|
typedef boost::timer::nanosecond_type nanosecond_t;
|
||||||
steady_clock::duration length = seconds(default_length);
|
std::string command_args;
|
||||||
|
uint64_t n; // number of test cases to run
|
||||||
|
int places = 3; // decimal places for times
|
||||||
|
bool verbose (false);
|
||||||
|
|
||||||
template <class T, class Endian>
|
struct result_type
|
||||||
uint32_t test(T x)
|
|
||||||
{
|
{
|
||||||
uint32_t count = 0;
|
nanosecond_t cpu_time; // system + user time
|
||||||
T y = x;
|
uint64_t v; // value computed; returning this may prevent
|
||||||
steady_clock::time_point start = steady_clock::now();
|
// optimizer from optimizing away the timing loop
|
||||||
steady_clock::time_point end = start + length;
|
};
|
||||||
while (steady_clock::now() < end)
|
|
||||||
|
void process_command_line(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
for (int a = 0; a < argc; ++a)
|
||||||
|
{
|
||||||
|
command_args += argv[a];
|
||||||
|
if (a != argc-1)
|
||||||
|
command_args += ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << command_args << '\n';;
|
||||||
|
|
||||||
|
if (argc >=2)
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
n = std::atoll(argv[1]);
|
||||||
|
#else
|
||||||
|
n = _atoi64(argv[1]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (; argc > 2; ++argv, --argc)
|
||||||
|
{
|
||||||
|
if ( *(argv[2]+1) == 'p' )
|
||||||
|
places = atoi( argv[2]+2 );
|
||||||
|
else if ( *(argv[2]+1) == 'v' )
|
||||||
|
verbose = true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "Error - unknown option: " << argv[2] << "\n\n";
|
||||||
|
argc = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
cout << "Usage: benchmark n [Options]\n"
|
||||||
|
" The argument n specifies the number of test cases to run\n"
|
||||||
|
" Options:\n"
|
||||||
|
" -v Verbose messages\n"
|
||||||
|
" -p# Decimal places for times; default -p" << places << "\n";
|
||||||
|
return std::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
template <class T, class EndianT>
|
||||||
|
result_type test_inc(T x)
|
||||||
|
{
|
||||||
|
cout << "++ a value..." << endl;
|
||||||
|
result_type result;
|
||||||
|
result.v = 0;
|
||||||
|
T y(x);
|
||||||
|
boost::timer::auto_cpu_timer t(places);
|
||||||
|
for (uint64_t i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
++y;
|
||||||
|
}
|
||||||
|
t.stop();
|
||||||
|
result.v = static_cast<uint64_t>(y);
|
||||||
|
boost::timer::cpu_times times = t.elapsed();
|
||||||
|
result.cpu_time = (times.system + times.user);
|
||||||
|
t.report();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class EndianT>
|
||||||
|
result_type test_rev_inc(T x)
|
||||||
|
{
|
||||||
|
cout << "reverse, then ++ a value..." << endl;
|
||||||
|
result_type result;
|
||||||
|
result.v = 0;
|
||||||
|
T y(x);
|
||||||
|
boost::timer::auto_cpu_timer t(places);
|
||||||
|
for (uint64_t i = 0; i < n; ++i)
|
||||||
{
|
{
|
||||||
++count;
|
|
||||||
reverse(y);
|
reverse(y);
|
||||||
++y;
|
++y;
|
||||||
reverse(y);
|
reverse(y);
|
||||||
}
|
}
|
||||||
|
t.stop();
|
||||||
|
result.v = static_cast<uint64_t>(y);
|
||||||
|
boost::timer::cpu_times times = t.elapsed();
|
||||||
|
result.cpu_time = (times.system + times.user);
|
||||||
|
t.report();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
cout << " loop executed " << count << " times" << endl;
|
template <class T, class EndianT>
|
||||||
// cout << " x is 0x" << std::hex << x << std::dec << endl;
|
result_type test_endian_inc(T x)
|
||||||
|
|
||||||
count = 0;
|
|
||||||
//Endian z(x);
|
|
||||||
Endian z;
|
|
||||||
start = steady_clock::now();
|
|
||||||
end = start + length;
|
|
||||||
while (steady_clock::now() < end)
|
|
||||||
{
|
{
|
||||||
++count;
|
cout << "++ an endian value..." << endl;
|
||||||
++z;
|
result_type result;
|
||||||
}
|
result.v = 0;
|
||||||
|
EndianT y(x);
|
||||||
cout << " loop executed " << count << " times" << endl;
|
boost::timer::auto_cpu_timer t(places);
|
||||||
|
for (uint64_t i = 0; i < n; ++i)
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t nop_test()
|
|
||||||
{
|
{
|
||||||
uint32_t count = 0;
|
++y;
|
||||||
steady_clock::time_point start = steady_clock::now();
|
}
|
||||||
steady_clock::time_point end = start + length;
|
t.stop();
|
||||||
while (steady_clock::now() < end)
|
result.v = static_cast<uint64_t>(y);
|
||||||
|
boost::timer::cpu_times times = t.elapsed();
|
||||||
|
result.cpu_time = (times.system + times.user);
|
||||||
|
t.report();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class EndianT>
|
||||||
|
void test(T x)
|
||||||
{
|
{
|
||||||
++count;
|
test_inc<T, EndianT>(x);
|
||||||
|
test_rev_inc<T, EndianT>(x);
|
||||||
|
test_endian_inc<T, EndianT>(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << " loop executed " << count << " times" << endl;
|
|
||||||
|
|
||||||
return count;
|
//result_type nop_test()
|
||||||
}
|
//{
|
||||||
|
// result_type result;
|
||||||
|
// result.v = 0;
|
||||||
|
// boost::timer::auto_cpu_timer t(places);
|
||||||
|
// for (uint64_t i = 0; i < n; ++i)
|
||||||
|
// {
|
||||||
|
// ++result.v;
|
||||||
|
// }
|
||||||
|
// t.stop();
|
||||||
|
// boost::timer::cpu_times times = t.elapsed();
|
||||||
|
// result.cpu_time = (times.system + times.user);
|
||||||
|
// t.report();
|
||||||
|
// return result;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
@@ -90,40 +177,27 @@ namespace
|
|||||||
|
|
||||||
int cpp_main(int argc, char* argv[])
|
int cpp_main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (argc > 1)
|
process_command_line(argc, argv);
|
||||||
length = seconds(atol(argv[1]));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cout << "Invoke: speed_test [s]\n"
|
|
||||||
" where s is the number of seconds each test will be run (default "
|
|
||||||
<< default_length << (default_length <= 1 ? " second)" : " seconds)") << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "\nbyte swap intrinsics used: " BOOST_ENDIAN_INTRINSIC_MSG << endl << endl;
|
cout << "\nbyte swap intrinsics used: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
|
||||||
|
|
||||||
//std::cerr << std::hex;
|
cout << endl << "int16_t, big16_t" << endl;
|
||||||
|
|
||||||
cout << "nop" << endl;
|
|
||||||
nop_test();
|
|
||||||
|
|
||||||
|
|
||||||
cout << "int16_t, big16_t" << endl;
|
|
||||||
test<int16_t, big16_t>(0x1122);
|
test<int16_t, big16_t>(0x1122);
|
||||||
|
|
||||||
cout << "uint16_t" << endl;
|
cout << endl << "int16_t, little16_t" << endl;
|
||||||
test<uint16_t, ubig16_t>(0x1122U);
|
test<int16_t, little16_t>(0x1122);
|
||||||
|
|
||||||
cout << "int32_t, big32_t" << endl;
|
cout << endl << "int32_t, big32_t" << endl;
|
||||||
test<int32_t, big32_t>(0x11223344);
|
test<int32_t, big32_t>(0x11223344);
|
||||||
|
|
||||||
cout << "uint32_t, ubig32_t" << endl;
|
cout << endl << "int32_t, little32_t" << endl;
|
||||||
test<uint32_t, ubig32_t>(0x11223344UL);
|
test<int32_t, little32_t>(0x11223344);
|
||||||
|
|
||||||
cout << "int64_t, big64_t" << endl;
|
cout << endl << "int64_t, big64_t" << endl;
|
||||||
test<int64_t, big64_t>(0x1122334455667788);
|
test<int64_t, big64_t>(0x1122334455667788);
|
||||||
|
|
||||||
cout << "uint64_t, ubig64_t" << endl;
|
cout << endl << "int64_t, little64_t" << endl;
|
||||||
test<uint64_t, ubig64_t>(0x1122334455667788ULL);
|
test<int64_t, little64_t>(0x1122334455667788);
|
||||||
|
|
||||||
//cout << "float" << endl;
|
//cout << "float" << endl;
|
||||||
//test<float>(1.2345f);
|
//test<float>(1.2345f);
|
||||||
|
Reference in New Issue
Block a user