我的 C++ 吗?编译器优化我的代码?
在使用现代 C++ 编译器(包括 MSVC、GCC、ICC)时,我该如何说它是否具有: 并行
- 化代码
- 向量化循环(或使用其他特定处理器指令)
- 展开检测到的循环
- 尾递归
- 执行 RVO(返回值优化) )
- 或以其他方式优化
而不深入研究编译器生成的汇编代码?
While using modern C++ compilers (including MSVC, GCC, ICC), how can I say if it has:
- parallelized the code
- vectorized the loops (or used other specific processor instructions)
- unrolled the loops
- detected tail-recursion
- performed RVO (return-value optimization)
- or optimized in some other way
without diving into the assembler code the compiler produces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以真正判断的唯一方法是检查汇编器输出(您似乎已经忽略了它)。除此之外,您可以阅读 doco 以了解编译器的每个级别提供哪些类型的优化。
但是,老实说,如果您不相信编译器的优化级别正在完成这项工作,您可能不会信任 doco :-)
我会自己查看汇编器,这是您可以做到的唯一方法确实确定。
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.
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 :-)
I would look at the assembler myself, it's the only way you could be truly certain.
英特尔编译器具有不错的报告功能。在参考文档或手册页中查找 -vec-report 和 -par-report。
g++也有向量报告,在手册页中查找“向量”,我不认为g++具有并行自动代码生成功能。
至于最后三件事,我认为编译器不会报告这一点,因此您可能必须转到汇编才能获取该信息
Intel compiler has decent reporting facility. Look up -vec-report and -par-report in reference documentation or in the man page.
g++also has vector reports, look in the man page for "vector", I'd don't think g++ has parallel automatic code generation.
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
对于 RVO 或其他复制省略的东西,只需将一些日志记录 (printf) 放入类的 copy-ctor 和 dtor 中即可。如果优化有效,您应该会看到更少的对象被复制。
For RVO or other copy-elision stuff, just put some logging (printf) in copy-ctor and dtor of your class. You should see fewer objects being copied around if optimizations are working.
我非常确定,如果您在编译器中使用最深度的优化,代码将被并行化,循环将被矢量化,并且许多其他矢量化技术也将起作用。
为了使用这么多的深度,请在运行代码时使用 -O3 命令。
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.