【发布时间】:2011-12-13 09:01:08
【问题描述】:
我有一些继承的内部 C++ 代码,在 Windows 上使用 VC++ 编译时,运行速度比在 Linux 上使用 g++ 编译时快一个数量级(5 分钟对 2 小时)。无论有没有“正常”优化标志,以及每个编译器和各自平台的几个不同版本,都在可比较的硬件上,情况仍然如此。
在 Linux 上使用 g++ 构建调试/配置文件版本 (-g -pg),我发现以下三个方面大部分时间都在消耗:
% cumulative self self total
time seconds seconds calls Ks/call Ks/call name
31.95 955.93 955.93 3831474321 0.00 0.00 std::_List_const_iterator<xxFile>::operator!=(std::_List_const_iterator<xxFile> const&) const
22.51 1629.64 673.71 3144944335 0.00 0.00 std::_List_const_iterator<xxFile>::operator++()
15.56 2095.29 465.65 686529986 0.00 0.00 std::iterator_traits<std::_List_const_iterator<dtFile> >::difference_type std::__distance<std::_List_const_iterator<xxFile> >(std::_List_const_iterator<xxFile>, std::_List_const_iterator<xxFile>, std::input_iterator_tag)
(xxFile 类由整数、浮点数、双精度数、布尔值和字符串组成)
我的天真猜测是 VC++ 正在补偿一些编码不佳的东西,或者 GNU STL 可能没有得到优化。我目前正在使用 Boost 库编译 g++/Linux 版本,从 assign/std/list.hpp 和 boost::assign 命名空间开始。
我无法分享代码,但根据您的经验,是否有明显的原因(除了我有限的 C++ 经验之外)?
【问题讨论】:
-
你是用
g++ -O2编译的吗?您是否使用了最新的 GCC 编译器(4.6.2)? -
是的,确实如此,但我无法分享答案 ;-)
-
我已经尝试过 -02 优化,我最近的尝试是在 Fedora 16(以及许多过去的版本)上使用 GCC 4.6.2。
-
我可以冒险猜测,但猜测通常/通常是错误的。您正在使用
gprof。不要使用gprof- 它不会告诉你真正的问题是什么。 Look here for reasons why, and an alternative. -
在 GCC(甚至 4.6.2)中有一些已知的错误,即 size() 不是 STL 集合的恒定时间。
标签: performance visual-c++ g++