[Insight-developers] STYLE: Un-necessary use of static_cast -- should we care?

Bradley Lowekamp blowekamp at mail.nih.gov
Tue Jul 24 08:07:07 EDT 2012


Hello,

Sorry for the need for a correction, the performance number below were from an un optimized build. Adding -O3 to the command line I get the following:

reinterpret_cast known-type:  with relative 1.00, fastest 4825730.00, average 4985316.17, stddev 126105.34
virtual function + reinterpret_cast:  with relative 1.69, fastest 8206468.00, average 8438137.33, stddev 208099.04
member variable + reinterpret_cast:  with relative 1.13, fastest 5501744.00, average 5629723.67, stddev 148042.93
dynamic_cast same-type-base success:  with relative 0.99, fastest 4824356.00, average 4939668.83, stddev 146437.49
dynamic_cast same-type-level1 success:  with relative 4.45, fastest 21898599.00, average 22188647.67, stddev 180007.58
dynamic_cast same-type-level2 success:  with relative 4.41, fastest 21479420.00, average 21995220.67, stddev 495072.22
dynamic_cast same-type-level3 success:  with relative 4.52, fastest 21890185.00, average 22530887.17, stddev 339243.49
dynamic_cast level1-to-base success:  with relative 1.15, fastest 5617575.00, average 5751829.33, stddev 129190.71
dynamic_cast level2-to-base success:  with relative 1.00, fastest 4902457.00, average 4986876.00, stddev 117571.92
dynamic_cast level3-to-base success:  with relative 1.00, fastest 4866250.00, average 4997263.67, stddev 128095.55
dynamic_cast level2-to-level1 success:  with relative 5.82, fastest 28435995.00, average 29027231.83, stddev 418624.95
dynamic_cast level3-to-level1 success:  with relative 7.51, fastest 36851934.00, average 37444322.50, stddev 565991.12
dynamic_cast level3-to-level2 success:  with relative 5.93, fastest 29244914.00, average 29575495.83, stddev 307082.04
dynamic_cast onebase-to-twobase fail:  with relative 3.36, fastest 16506576.00, average 16770324.17, stddev 283799.28
dynamic_cast onelevel1-to-twobase fail:  with relative 5.12, fastest 25358976.00, average 25537256.33, stddev 397193.42
dynamic_cast onelevel2-to-twobase fail:  with relative 6.87, fastest 33564081.00, average 34226014.83, stddev 513892.90
dynamic_cast onelevel3-to-twobase fail:  with relative 8.48, fastest 41400782.00, average 42276741.00, stddev 675367.78
ostringstream:  with relative 349.10, fastest 1648423719.00, average 1740360830.50, stddev 46542301.52
sprintf:  with relative 121.50, fastest 603019646.00, average 605695869.00, stddev 2756975.84

Brad


On Jul 23, 2012, at 9:24 PM, Lowekamp, Bradley (NIH/NLM/LHC) [C] wrote:

> Hello Kent,
> 
> First:
> 
> I have not really tried this but I just prototyped it and it could be useful if you want to try to track down the cases a little more programmatically, and care to mess with tr1 typetraits a little. Consider adding the following to itkMacro.h:
> 
> 
> #include <tr1/type_traits>
> #include <tr1/type_compare>
> 
> template < typename TOuttype, typename TIntype >
> TOuttype itk_dynamic_cast( TIntype in )
> {
> static_assert( std::tr1::is_pointer<TOuttype>::value );
> static_assert( std::tr1::is_base_of<TIntype, TOuttype>::value );
> 
> return dynamic_cast<TOuttype>( in );
> }
> 
> #define dynamic_cast itk_dynamic_cast
> 
> Second:
> 
> While I am aware the use of dynamic_cast is not high performance it didn't bother me being used in for this case. So I modified the performance test Matt linked with the following additional cases:
> 
>       res = 0;
>       start = getticks();
>       for (size_t i=0 ; i<N ; ++i) {
>         const char baseName[] = "IndexedDataObject";
>         std::ostringstream oss;
>         oss << baseName << N;
>         std::string s = oss.str();
>         res += s.size();
>       }
>       end = getticks();
>       timed = elapsed(end, start);
>       timings[17].push_back(timed);
> 
>       res = 0;
>       start = getticks();
>       for (size_t i=0 ; i<N ; ++i) {
>         char buf[100];
>         sprintf(buf, "IndexedDataObject%u", static_cast<unsigned int>(N));
>         std::string s(buf);
>         res += s.size();
>       }
>       end = getticks();
>       timed = elapsed(end, start);
>       timings[18].push_back(timed);
> 
> These is based on code that is in the GetInput methods. This following is the results on my i7 laptop:
> 
> reinterpret_cast known-type:  with relative 1.00, fastest 11686168.00, average 12120401.33, stddev 298867.57
> virtual function + reinterpret_cast:  with relative 1.78, fastest 21040604.00, average 21536555.33, stddev 464657.48
> member variable + reinterpret_cast:  with relative 1.10, fastest 13127058.00, average 13311370.00, stddev 227979.57
> dynamic_cast same-type-base success:  with relative 0.97, fastest 11514018.00, average 11717135.50, stddev 125301.84
> dynamic_cast same-type-level1 success:  with relative 2.94, fastest 34896278.00, average 35578390.00, stddev 393787.56
> dynamic_cast same-type-level2 success:  with relative 2.88, fastest 34403850.00, average 34912766.67, stddev 516410.80
> dynamic_cast same-type-level3 success:  with relative 2.84, fastest 33781284.00, average 34444748.67, stddev 593710.46
> dynamic_cast level1-to-base success:  with relative 1.05, fastest 12517034.00, average 12768522.83, stddev 322468.58
> dynamic_cast level2-to-base success:  with relative 1.01, fastest 12024095.00, average 12287761.83, stddev 245272.40
> dynamic_cast level3-to-base success:  with relative 1.03, fastest 12074146.00, average 12427744.83, stddev 386862.93
> dynamic_cast level2-to-level1 success:  with relative 3.28, fastest 39064702.00, average 39723110.50, stddev 601394.77
> dynamic_cast level3-to-level1 success:  with relative 3.93, fastest 46206012.00, average 47576171.83, stddev 1193834.23
> dynamic_cast level3-to-level2 success:  with relative 3.38, fastest 40250794.00, average 40943911.83, stddev 454434.63
> dynamic_cast onebase-to-twobase fail:  with relative 2.00, fastest 23874560.00, average 24276072.83, stddev 1132888.98
> dynamic_cast onelevel1-to-twobase fail:  with relative 2.57, fastest 30602545.00, average 31203997.50, stddev 527747.22
> dynamic_cast onelevel2-to-twobase fail:  with relative 3.08, fastest 37019228.00, average 37358361.00, stddev 269371.47
> dynamic_cast onelevel3-to-twobase fail:  with relative 3.74, fastest 44848942.00, average 45287361.83, stddev 584046.88
> ostringstream:  with relative 144.16, fastest 1705792767.00, average 1747226021.83, stddev 20540335.54
> sprintf:  with relative 47.74, fastest 575666370.00, average 578647874.17, stddev 3708284.83
> 
> 
> Brad



More information about the Insight-developers mailing list