Boost logo

Boost Users :

Subject: [Boost-users] [boost.numeric] Poor Performance of numeric_cast
From: Tang Jiang Jun (tangjiangjun_at_[hidden])
Date: 2012-10-15 04:16:56


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 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