Closed Leviathan1995 closed 7 years ago
@gaodq right.
@gaodq @baotiao 我专门写了个测试,循环一亿次,如果类型是系统的基本类型,运算符前置后置效率相差很小,如果是自定义类型或者STL标准库,性能会相差比较大。
这是测试代码:https://gist.github.com/Leviathan1995/dced964f489ebc9cd0592063ed80c066
测试结果:
test 0
int postfix 814ms
int prefix 751ms
vector postfix 5404ms
vector prefix 4011ms
test 1
int postfix 771ms
int prefix 764ms
vector postfix 5529ms
vector prefix 3669ms
test 2
int postfix 744ms
int prefix 721ms
vector postfix 5313ms
vector prefix 3633ms
test 3
int postfix 737ms
int prefix 745ms
vector postfix 5005ms
vector prefix 3616ms
test 4
int postfix 749ms
int prefix 746ms
vector postfix 5305ms
vector prefix 3704ms
试试 -O2 优化,我这试了优化后相差不大了。
循环里的自增编译器会做优化,而且没有太影响性能,不必改成这样了