vmware虚拟机(5800X3D,给虚拟机分配的4核。虚拟机不适合测性能,只参考)
happy@ubuntu:~$ g++ -std=c++11 -O3 -pthread 1.cpp
happy@ubuntu:~$ ./a.out
Atomic counter: 8000000 Time: 71 ms
Non-atomic counter: 1444110 Time: 68 ms
happy@ubuntu:~$ ./a.out
Atomic counter: 8000000 Time: 76 ms
Non-atomic counter: 1783212 Time: 72 ms
happy@ubuntu:~$ ./a.out
Atomic counter: 8000000 Time: 75 ms
Non-atomic counter: 1355109 Time: 74 ms
老机器i7-4790k上,真机:
happy@happy:~$ g++ -std=c++11 -O3 -pthread 1.cpp
happy@happy:~$ ./a.out
Atomic counter: 8000000 Time: 139 ms
Non-atomic counter: 2049804 Time: 14 ms
happy@happy:~$ ./a.out
Atomic counter: 8000000 Time: 140 ms
Non-atomic counter: 3005134 Time: 8 ms
happy@happy:~$ ./a.out
Atomic counter: 8000000 Time: 140 ms
Non-atomic counter: 1551018 Time: 5 ms
happy@happy:~$ ./a.out
Atomic counter: 8000000 Time: 134 ms
Non-atomic counter: 1232533 Time: 5 ms
happy@happy:~$
一条带lock的add指令:
0000000000401120 <non_atomic_increment(int volatile&)>:
401120: ba 40 42 0f 00 mov $0xf4240,%edx
401125: 0f 1f 00 nopl (%rax)
401128: 8b 07 mov (%rdi),%eax
40112a: 83 c0 01 add $0x1,%eax
40112d: 89 07 mov %eax,(%rdi)
40112f: 83 ea 01 sub $0x1,%edx
401132: 75 f4 jne 401128 <non_atomic_increment(int volatile&)+0x8>
401134: c3 ret
401135: 66 66 2e 0f 1f 84 00 data16 cs nopw 0x0(%rax,%rax,1)
40113c: 00 00 00 00
0000000000401140 <atomic_increment(std::atomic<int>&)>:
401140: b8 40 42 0f 00 mov $0xf4240,%eax
401145: 0f 1f 00 nopl (%rax)
401148: f0 83 07 01 lock addl $0x1,(%rdi)
40114c: 83 e8 01 sub $0x1,%eax
40114f: 75 f7 jne 401148 <atomic_increment(std::atomic<int>&)+0x8>
401151: c3 ret
--
修改:z16166 FROM 221.218.163.*
FROM 221.218.163.*