我嘗試以下:C++ bitset不內聯?
#include <bitset>
int main()
{
std::bitset<32> bit32 { 0xf0f0f0f0 };
bit32[4].flip();
bit32[0] = 1;
}
使用GCC 4.8.1,我無法得到它內聯,objdump的給我或多或少:
401536: c7 45 e4 f0 f0 f0 f0 movl $0xf0f0f0f0,-0x1c(%ebp)
40153d: 8d 45 e8 lea -0x18(%ebp),%eax
401540: c7 44 24 04 04 00 00 movl $0x4,0x4(%esp)
401547: 00
401548: 8d 55 e4 lea -0x1c(%ebp),%edx
40154b: 89 14 24 mov %edx,(%esp)
40154e: 89 c1 mov %eax,%ecx
401550: e8 93 13 00 00 call 4028e8 <__ZNSt6bitsetILj32EEixEj>
401555: 83 ec 08 sub $0x8,%esp
401558: 8d 45 e8 lea -0x18(%ebp),%eax
40155b: 89 c1 mov %eax,%ecx
40155d: e8 b2 12 00 00 call 402814 <__ZNSt6bitsetILj32EE9reference4flipEv>
401562: 8d 45 e8 lea -0x18(%ebp),%eax
401565: 89 c1 mov %eax,%ecx
401567: e8 10 13 00 00 call 40287c <__ZNSt6bitsetILj32EE9referenceD1Ev>
40156c: 8d 45 f0 lea -0x10(%ebp),%eax
40156f: c7 44 24 04 00 00 00 movl $0x0,0x4(%esp)
401576: 00
401577: 8d 55 e4 lea -0x1c(%ebp),%edx
40157a: 89 14 24 mov %edx,(%esp)
40157d: 89 c1 mov %eax,%ecx
40157f: e8 64 13 00 00 call 4028e8 <__ZNSt6bitsetILj32EEixEj>
401584: 83 ec 08 sub $0x8,%esp
401587: 8d 45 f0 lea -0x10(%ebp),%eax
40158a: c7 04 24 01 00 00 00 movl $0x1,(%esp)
401591: 89 c1 mov %eax,%ecx
401593: e8 f0 12 00 00 call 402888 <__ZNSt6bitsetILj32EE9referenceaSEb>
401598: 83 ec 04 sub $0x4,%esp
40159b: 8d 45 f0 lea -0x10(%ebp),%eax
40159e: 89 c1 mov %eax,%ecx
4015a0: e8 d7 12 00 00 call 40287c <__ZNSt6bitsetILj32EE9referenceD1Ev>
4015a5: b8 00 00 00 00 mov $0x0,%eax
4015aa: 8b 4d fc mov -0x4(%ebp),%ecx
-O1,-O2, -O3也沒有幫助,它完全優化代碼。 反正我可以避免調用函數嗎?
爲什麼調用函數會導致問題? –
@EdHeal:我確定它比它應該優化的速度慢得多(例如,一個'movl'指令,就像它爲我所做的那樣)。對於這個微小的測試用例來說這可能不是什麼大問題,但是如果它在某個地方的循環中處於性能危急的代碼中,那將會產生所有差異。 –
所以,你的問題是,代碼是未優化的,除非你告訴編譯器優化,在這種情況下,它優化它? – Casey