|
Boost Users : |
Subject: Re: [Boost-users] [boost.numeric] Poor Performance of numeric_cast
From: Oswin Krause (Oswin.Krause_at_[hidden])
Date: 2012-10-15 06:43:10
Hi,
Never benchmark in debug mode. Moreover, never ever benchmark boost
code in debug mode.
On 2012-10-15 11:29, Tang Jiang Jun wrote:
> Hi Oswin,
>
> Sorry, I forgot to mention that I compiled it as debug configuration
> in order to prevent unintended optimization.
> Anyway, many thanks for reminding!
>
> Tang
> Â
>
> On Mon, Oct 15, 2012 at 4:30 PM, Oswin Krause
> <Oswin.Krause_at_[hidden] [3]> wrote:
>
>> Hi,
>>
>> Your complete loop got optimized away in the native test cases.
>> Because of the try/catch block the compiler couldn't do this in the
>> other cases. So you are benchmarking nothing vs somthing.
>>
>> Greetings,
>> Oswin
>>
>> On 2012-10-15 10:16, Tang Jiang Jun wrote:
>>
>>> Hi,
>>>
>>> I have run a performance testing for numeric_cast recently, and
>>> found
>>> that the result was really unexpected bad, although the document
>>> mentioned that it will be no overhead if overflows don't happen.
>>> Could somebody please help me to verify this testing? If this is
>>> true,
>>> I doubt whether I should use numeric_cast in the production code.
>>>
>>> Here is my testing code and result.
>>>
>>> #include <boost/numeric/conversion/cast.hpp>
>>> #include <boost/format.hpp>
>>> #include <boost/cstdint.hpp>
>>> Â #include <boost/chrono.hpp>
>>> #include <iostream>
>>>
>>> using namespace std;
>>> using namespace boost;
>>> using namespace boost::numeric;
>>> using namespace boost::chrono;
>>>
>>> int main()
>>> {
>>> Â Â Â const static int32_t COUNT = 1000000;
>>> Â Â Â high_resolution_clock::time_point start;
>>>
>>> Â Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â int32_t i32 = 123;
>>> Â Â Â Â Â Â int16_t i16 = i32;
>>> Â Â Â }
>>> Â Â Â cout << format("Native Integer Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â try
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â int32_t i32 = 100;
>>> Â Â Â Â Â Â Â Â Â int16_t i16 = numeric_cast< int16_t >( i32
>>> );
>>> Â Â Â Â Â Â Â }
>>> Â Â Â Â Â Â catch( const bad_numeric_cast& e )
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â cout << e.what() << endl;
>>> Â Â Â Â Â Â }
>>> Â Â Â }
>>> Â Â Â cout << format("Boost Integer Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â float f = 100.0f;
>>> Â Â Â Â Â Â int32_t i = static_cast< int32_t >( f );
>>> Â Â Â }
>>> Â Â Â cout << format("Native Floating-Integer Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â try
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â float f = 123.0f;
>>> Â Â Â Â Â Â Â Â Â int32_t i = numeric_cast< int32_t >( f );
>>> Â Â Â Â Â Â Â }
>>> Â Â Â Â Â Â catch( const bad_numeric_cast& e )
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â cout << e.what() << endl;
>>> Â Â Â Â Â Â }
>>> Â Â Â }
>>> Â Â Â cout << format("Boost Floating-Integer Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â int32_t i = 132;
>>> Â Â Â Â Â Â float f = static_cast< float >( i );
>>> Â Â Â }
>>> Â Â Â cout << format("Native Integer-Floating Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â start = high_resolution_clock::now();
>>> Â Â Â for( int32_t n = 0; n < COUNT; ++n )
>>> Â Â Â {
>>> Â Â Â Â Â Â try
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â int32_t i = 128;
>>> Â Â Â Â Â Â Â Â Â float f = numeric_cast< float >( i );
>>> Â Â Â Â Â Â Â }
>>> Â Â Â Â Â Â catch( const bad_numeric_cast& e )
>>> Â Â Â Â Â Â {
>>> Â Â Â Â Â Â Â Â Â cout << e.what() << endl;
>>> Â Â Â Â Â Â }
>>> Â Â Â }
>>> Â Â Â cout << format("Boost Integer-Floating Cast: %1%n") % ( (
>>>
>>> high_resolution_clock::now() - start ) / COUNT );
>>>
>>> Â Â Â return 0;
>>> };
>>>
>>> Result:
>>> Native Integer Cast: 3 nanoseconds
>>> Boost Integer Cast: 311 nanoseconds
>>> Native Floating-Integer Cast: 4 nanoseconds
>>> Â Boost Floating-Integer Cast: 430 nanoseconds
>>> Native Integer-Floating Cast: 2 nanoseconds
>>> Boost Integer-Floating Cast: 106 nanoseconds
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden] [1]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users [2]
>
>
>
> Links:
> ------
> [1] mailto:Boost-users_at_[hidden]
> [2] http://lists.boost.org/mailman/listinfo.cgi/boost-users
> [3] mailto:Oswin.Krause_at_[hidden]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net