简体   繁体   English

我的C ++编译器是否优化了我的代码?

[英]Does my C++ compiler optimize my code?

While using modern C++ compilers (including MSVC, GCC, ICC), how can I say if it has: 在使用现代C ++编译器(包括MSVC,GCC,ICC)时,我怎么说它是否有:

  1. parallelized the code 并行化代码
  2. vectorized the loops (or used other specific processor instructions) 矢量化循环(或使用其他特定的处理器指令)
  3. unrolled the loops 展开循环
  4. detected tail-recursion 检测到尾递归
  5. performed RVO (return-value optimization) 执行RVO(返回值优化)
  6. or optimized in some other way 或以其他方式优化

without diving into the assembler code the compiler produces? 没有深入编译器产生的汇编代码?

The only way you can really tell is if you examine the assembler output (which you appear to have discounted). 您可以真正判断的唯一方法是检查汇编器输出(您似乎已打折)。 Other than that, you could read the doco to see what types of optimization each level of your compiler provides. 除此之外,您可以阅读doco以了解编译器的每个级别提供的优化类型。

But, in all honesty, if you don't trust that the optimization levels of your compiler are doing the job, you probably won't trust the doco :-) 但是,老实说,如果你不相信你的编译器的优化级别正在完成工作,你可能不会相信doco :-)

I would look at the assembler myself, it's the only way you could be truly certain. 我自己会看看汇编程序,这是你真正确定的唯一方法。

Intel compiler has decent reporting facility. Intel编译器具有良好的报告功能。 Look up -vec-report and -par-report in reference documentation or in the man page. 在参考文档或手册页中查找-vec-report和-par-report。

g++also has vector reports, look in the man page for "vector", I'd don't think g++ has parallel automatic code generation. g ++也有矢量报告,在man页面查看“vector”,我不认为g ++有并行自动代码生成。

As far as last three things, I'd don't think compilers report that, so you probably have to go to assembly to get that information 至于最后三件事,我不认为编译器报告这一点,所以你可能不得不去汇编来获取这些信息

For RVO or other copy-elision stuff, just put some logging (printf) in copy-ctor and dtor of your class. 对于RVO或其他复制内容,只需在您的班级的copy-ctor和dtor中添加一些日志记录(printf)。 You should see fewer objects being copied around if optimizations are working. 如果优化正在运行,您应该看到更少的对象被复制。

I'm pretty sure that if you use the most depth optimization in your compiler the code will be parallelized and the loops will be vectorized and many other vectorization techniques will work too. 我很确定如果你在编译器中使用最深度优化,代码将被并行化,循环将被矢量化,许多其他矢量化技术也将起作用。

In order to use that much depth, use -O3 command when you run your code. 为了使用这么多深度,请在运行代码时使用-O3命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM