Try to see which cast is faster (not necessary better): new c++ case or old fashion C style cast. Any ideas?
试着看看哪个演员表更快(不一定更好):新的c ++案例或旧时尚C风格演员。有任何想法吗?
7 个解决方案
#1
33
There should be no difference at all if you compare int()
to equivalent functionality of static_cast<int>()
.
如果将int()与static_cast
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!
显然,它是100%相同!
#2
3
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.
当谈到这样的基本构造作为单个演员时,一旦两个构造具有相同的语义含义,它们的性能将完全相同,并且为这些构造生成的机器代码将是相同的。
#3
3
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.
我相信实际结果是实现定义的。您应该在您的编译器版本中进行检查。但我相信它会在大多数现代编译器中产生相同的结果。在C ++中,你不应该使用C-cast,而是使用C ++强制转换 - 它允许你在编译时发现错误。
#4
2
Take a look at the assembly using each method. If it differs use a profiler.
使用每种方法查看装配。如果不同则使用分析器。
#5
1
They are same as it is resolved during compile time itself and there is no runtime overhead. Even if there was some difference I really wouldn't bother too much about these tiny (not even micro) optimizations.
它们与在编译时自身解析的相同,并且没有运行时开销。即使存在一些差异,我也不会过分关注这些微小的(甚至微观的)优化。
#6
0
As most people say one hopes these should be the same speed, although you're at the mercy of your compiler... and that's not always a very happy situation. Read on for war stories.
正如大多数人所说的那样,人们希望这些速度应该是相同的速度,尽管你受到编译器的支配......而且这并不总是一种非常快乐的情况。继续阅读战争故事。
Depending on your compiler and the particular model of processor core which the program executes on the speed of float f; int i(f);
, float f; int i = (int)f;
and float f; int i = static_cast<int>(f);
and their ilk (including variations involving double, long and unsigned types) can be atrociously slow - an order of magnitude worse than you expect. The compiler may emit instructions altering internal processor modes causing instruction pipelines to be thrown away. This is, in effect, a bug in the optimization element of the compiler. I've seen cases where one suffers the sort 40-clock-cycle costs mentioned in this analysis, at which point you have a major, unexpected and irritating performance bottleneck with AFAIK no entirely pleasing, robust, generic solution. There are alternatives involving assembler but AFAIK they do not round floating point to integer the same way as the casts do. If anyone knows any better I am interested. I'm hoping this issue is/will shortly be confined to legacy compilers/hardware but you need your wits about you.
取决于您的编译器和程序以浮点f的速度执行的特定处理器内核模型; int i(f);, float f; int i =(int)f;和浮动f; int i = static_cast
P.S. I can't reach that link because my firewall blocks it as games-related but a Google cache of it suffices to demonstrate that its author knows more about it than I do.
附:我无法访问该链接,因为我的防火墙阻止它与游戏相关,但它的谷歌缓存足以证明其作者比我更了解它。
#7
-2
When you choice makes little difference to the code, I'd pick the one which looks more familiar to later programmers. Making code easier to understand by others is always worth considering. In this case, I'd stick to int(…)
for that reason.
当你选择对代码没有什么区别时,我会选择一个对后来的程序员来说更熟悉的代码。让代码更易于理解的代码总是值得考虑的。在这种情况下,我坚持使用int(...)。
#1
33
There should be no difference at all if you compare int()
to equivalent functionality of static_cast<int>()
.
如果将int()与static_cast
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!
显然,它是100%相同!
#2
3
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.
当谈到这样的基本构造作为单个演员时,一旦两个构造具有相同的语义含义,它们的性能将完全相同,并且为这些构造生成的机器代码将是相同的。
#3
3
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.
我相信实际结果是实现定义的。您应该在您的编译器版本中进行检查。但我相信它会在大多数现代编译器中产生相同的结果。在C ++中,你不应该使用C-cast,而是使用C ++强制转换 - 它允许你在编译时发现错误。
#4
2
Take a look at the assembly using each method. If it differs use a profiler.
使用每种方法查看装配。如果不同则使用分析器。
#5
1
They are same as it is resolved during compile time itself and there is no runtime overhead. Even if there was some difference I really wouldn't bother too much about these tiny (not even micro) optimizations.
它们与在编译时自身解析的相同,并且没有运行时开销。即使存在一些差异,我也不会过分关注这些微小的(甚至微观的)优化。
#6
0
As most people say one hopes these should be the same speed, although you're at the mercy of your compiler... and that's not always a very happy situation. Read on for war stories.
正如大多数人所说的那样,人们希望这些速度应该是相同的速度,尽管你受到编译器的支配......而且这并不总是一种非常快乐的情况。继续阅读战争故事。
Depending on your compiler and the particular model of processor core which the program executes on the speed of float f; int i(f);
, float f; int i = (int)f;
and float f; int i = static_cast<int>(f);
and their ilk (including variations involving double, long and unsigned types) can be atrociously slow - an order of magnitude worse than you expect. The compiler may emit instructions altering internal processor modes causing instruction pipelines to be thrown away. This is, in effect, a bug in the optimization element of the compiler. I've seen cases where one suffers the sort 40-clock-cycle costs mentioned in this analysis, at which point you have a major, unexpected and irritating performance bottleneck with AFAIK no entirely pleasing, robust, generic solution. There are alternatives involving assembler but AFAIK they do not round floating point to integer the same way as the casts do. If anyone knows any better I am interested. I'm hoping this issue is/will shortly be confined to legacy compilers/hardware but you need your wits about you.
取决于您的编译器和程序以浮点f的速度执行的特定处理器内核模型; int i(f);, float f; int i =(int)f;和浮动f; int i = static_cast
P.S. I can't reach that link because my firewall blocks it as games-related but a Google cache of it suffices to demonstrate that its author knows more about it than I do.
附:我无法访问该链接,因为我的防火墙阻止它与游戏相关,但它的谷歌缓存足以证明其作者比我更了解它。
#7
-2
When you choice makes little difference to the code, I'd pick the one which looks more familiar to later programmers. Making code easier to understand by others is always worth considering. In this case, I'd stick to int(…)
for that reason.
当你选择对代码没有什么区别时,我会选择一个对后来的程序员来说更熟悉的代码。让代码更易于理解的代码总是值得考虑的。在这种情况下,我坚持使用int(...)。