Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which cast is faster static_cast<int> () or int()

Tags:

c++

performance

c

Try to see which cast is faster (not necessary better): new c++ case or old fashion C style cast. Any ideas?

like image 769
vehomzzz Avatar asked Oct 22 '09 17:10

vehomzzz


3 Answers

There should be no difference at all if you compare int() to equivalent functionality of static_cast<int>().

Using VC2008:

    double d = 10.5;
013A13EE  fld         qword ptr [__real@4025000000000000 (13A5840h)] 
013A13F4  fstp        qword ptr [d] 
    int x = int(d);
013A13F7  fld         qword ptr [d] 
013A13FA  call        @ILT+215(__ftol2_sse) (13A10DCh) 
013A13FF  mov         dword ptr [x],eax 
    int y = static_cast<int>(d);
013A1402  fld         qword ptr [d] 
013A1405  call        @ILT+215(__ftol2_sse) (13A10DCh) 
013A140A  mov         dword ptr [y],eax 

Obviously, it is 100% the same!

like image 112
Khaled Alshaya Avatar answered Oct 18 '22 06:10

Khaled Alshaya


No difference whatsoever.

When it comes to such basic constructs as a single cast, once two constructs have the same semantic meaning, their performace will be perfectly identical, and the machine code generated for these constructs will be the same.

like image 36
AnT Avatar answered Oct 18 '22 06:10

AnT


I believe that the actual result is implementation defined. You should check it in your version of compiler. But I believe that it will give the same result in most modern compilers. And in C++ you shouldn't use C-cast, instead use C++ casts - it will allow you to find errors at compile time.

like image 38
big-z Avatar answered Oct 18 '22 06:10

big-z