我正在编写一个 Ruby C 扩展,用于计算一组顶点的“软选择”。计算 3d 点之间的距离需要进行多次迭代。
最初我使用 Pelles C IDE - 基于我找到的模板。
然后我进行了更新,改用 Visual Studio C++ Express 2010 附带的 nmake
。我发现性能下降了 - 这很奇怪,因为如果有什么不同的话,它应该更快.
然后我恢复到用 Pelles C 编写的原始代码并使用 nmake
编译它,发现完全相同的代码速度较慢。
佩莱斯 C
> Updating soft selection took 0.741 seconds (12176 of 21692 Vertices)
> Updating soft selection took 0.751 seconds (10911 of 21692 Vertices)
> Updating soft selection took 0.859 seconds (10765 of 21692 Vertices)
> Updating soft selection took 0.753 seconds (10653 of 21692 Vertices)
> Updating soft selection took 0.75 seconds (10747 of 21692 Vertices)
> Updating soft selection took 0.751 seconds (10822 of 21692 Vertices)
Visual Studio
> Updating soft selection took 1.282 seconds (11853 of 21692 Vertices)
> Updating soft selection took 1.273 seconds (12204 of 21692 Vertices)
> Updating soft selection took 1.286 seconds (11720 of 21692 Vertices)
> Updating soft selection took 1.248 seconds (12996 of 21692 Vertices)
> Updating soft selection took 1.293 seconds (10705 of 21692 Vertices)
> Updating soft selection took 1.276 seconds (12204 of 21692 Vertices)
我对 C 和编译非常缺乏经验 - 但我认为性能差异是由于编译器和编译指令之间的差异造成的?
对于 nmake
版本,我使用了 extconf.rb
生成的 Makefile - 对于 Pelles C 版本,我使用了我找到的示例项目的任何设置。
我说的对吗 CFLAGS
在这里很重要?
CFLAGS?
对于 Pelles C 项目来说是:
CCFLAGS = -Tx86-coff -MD -Ot -Ox -W1 -Gd -Ze -Zl#
对于nmake
项目来说是:
CFLAGS = -MD -Zi -O2b2xg- -G6
当我查找 CFLAGS
和性能时,它通常会提到标志 O
、O2
和 O3
- 现在我在 nmake
Makefile 中看到一个 O2
,但带有一组奇怪的尾随字符。
Pelles C 项目有 Ot
和 Ox
... ?
我无法理解这些的含义。该扩展将在 Windows 和 OSX(PPC 和 Intel)下编译。我可以对编译器进行什么配置才能获得最大性能?至少恢复我以前的表现。
Makefile 和 Pelles C 配置
这里是 nmake
Makefile 的粘贴:http://pastie.org/3543595
这是 Pelles C 项目文件的粘贴: http://pastie.org/3543597
最佳答案
好的,正在查资料。我了解到,CFLAGS
选项取决于编译器。
我找到了 MS 的 cl
编译器的选项:http://msdn.microsoft.com/en-us/library/fwkeyyhe%28v=vs.80%29.aspx
我将它们与 Pelles C 帮助文件中记录的选项进行了比较。
使用这些CFLAGS
重新编译:
$CFLAGS = '-MD -Ot -Ox -W1'
重新编译后的性能结果:
> Updating soft selection took 0.679 seconds (12032 of 21692 Vertices)
> Updating soft selection took 0.607 seconds (13470 of 21692 Vertices)
> Updating soft selection took 0.717 seconds (13587 of 21692 Vertices)
> Updating soft selection took 0.613 seconds (13218 of 21692 Vertices)
> Updating soft selection took 0.635 seconds (9964 of 21692 Vertices)
> Updating soft selection took 0.746 seconds (10765 of 21692 Vertices)
瞧!性能恢复 - 甚至看起来稍微快一些。 :D
甚至消除了有关未知选项 -G6
的警告和其他一些过时的标志。
关于c - 切换编译器 - 性能下降 - 试图理解原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608629/