Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost.numeric] Poor Performance of numeric_cast
From: John Maddock (boost.regex_at_[hidden])
Date: 2012-10-16 04:12:06


> results:
>
> Native Integer Cast: 26729 nanoseconds
> Boost Integer Cast: 26449 nanoseconds
> Native Integer-Floating Cast: 105479 nanoseconds
> Boost Integer-Floating Cast: 105455 nanoseconds
> Native Floating-Integer Cast: 168933 nanoseconds
> Boost Floating-Integer Cast: 453505 nanoseconds
>
>
> so no overhead in Integer-Integer or Integer-Floating. But
> Floating-Integer has bad performance.

That's sort of what I would expect - think about it - if the cast is to the
same or a wider type, then there is no check, and numeric_cast and
static_cast do the same thing. However for a narrowing cast (float to
integer), then at the very least there has to be an extra if statement to
test whether the value being cast is in range - that would normally roughly
double the runtime cost. But there *may* be another hidden cost: depending
on the loop the compiler may decide not to inline the numeric_cast in order
to give a tighter loop, and the cost of the function call would add a big
chunk of time. Plus the extra code associated with the error handling if
the value is out of range adds a certain amount of code bloat to the loop,
reducing code locality. But the thing is you can't avoid this if you want
the runtime check. There's no such thing as a free lunch sadly.

John.


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