1
在Rust by Example #36中,在命令式和函数式中都计算达到极限的奇数整数之和。函数式编程中的开销
予分离这两个出并增加了上限10000000000000000和计时的结果:
祈使式:
me.home:rust_by_example>time ./36_higher_order_functions_a
Find the sum of all the squared odd numbers under 10000000000000000
imperative style: 333960700851149440
real 0m2.396s
user 0m2.387s
sys 0m0.009s
功能样式:
me.home:rust_by_example>time ./36_higher_order_functions_b
Find the sum of all the squared odd numbers under 10000000000000000
functional style: 333960700851149440
real 0m5.192s
user 0m5.188s
sys 0m0.003s
功能版本运行速度较慢并且编译所需的时间也稍长一些。
我的问题是,什么导致功能版本变慢?这是功能风格所固有的,还是由于编译器没有进行优化而导致的?
您必须编译优化(rustc -O ...) – Arjan 2014-09-02 17:57:21
当然。功能风格现在略快。 – user2664470 2014-09-02 18:29:03