jeudi 1 janvier 2015

Accessing stack variable slower than dereferencing pointer?

I ran two tests which perform the same operation: Add N random numbers. Test_1 uses an int on the stack, Test_2 uses an int on the heap. Surprisingly, on my machine Test_1 runs at around 945ms, Test_2 runs around 915ms (while timings may vary, the difference is very consistent and noticeable). What would explain such a significant difference? I used g++ with -O2 and -O3.



#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <chrono>

using namespace std;

int main()
{
constexpr int N = 100000000;
int x = 0; // TEST_1
//int* p = new int; //TEST_2
auto start_time = chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i)
{
x += rand(); //TEST_1
//*p += rand(); // TEST_2
}
auto end_time = chrono::high_resolution_clock::now();

cout << x << endl; // TEST_1
//cout << *p << endl; //TEST_2
cout << "Time: ";
cout << chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count() << endl;
}


Test 1 Assembly:



.file "main.cpp"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Time: "
.section .text.unlikely,"ax",@progbits
.LCOLDB1:
.section .text.startup,"ax",@progbits
.LHOTB1:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB1578:
.cfi_startproc
pushq %r12
.cfi_def_cfa_offset 16
.cfi_offset 12, -16
pushq %rbp
.cfi_def_cfa_offset 24
.cfi_offset 6, -24
xorl %ebp, %ebp
pushq %rbx
.cfi_def_cfa_offset 32
.cfi_offset 3, -32
movl $100000000, %ebx
call _ZNSt6chrono3_V212system_clock3nowEv
movq %rax, %r12
.p2align 4,,10
.p2align 3
.L2:
call rand
addl %eax, %ebp
subl $1, %ebx
jne .L2
call _ZNSt6chrono3_V212system_clock3nowEv
movl %ebp, %esi
movl $_ZSt4cout, %edi
movq %rax, %rbx
call _ZNSolsEi
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movq %rbx, %rcx
movabsq $4835703278458516699, %rdx
movl $_ZSt4cout, %edi
subq %r12, %rcx
movq %rcx, %rax
sarq $63, %rcx
imulq %rdx
sarq $18, %rdx
movq %rdx, %rsi
subq %rcx, %rsi
call _ZNSo9_M_insertIlEERSoT_
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
popq %rbx
.cfi_def_cfa_offset 24
xorl %eax, %eax
popq %rbp
.cfi_def_cfa_offset 16
popq %r12
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1578:
.size main, .-main
.section .text.unlikely
.LCOLDE1:
.section .text.startup
.LHOTE1:
.section .text.unlikely
.LCOLDB2:
.section .text.startup
.LHOTB2:
.p2align 4,,15
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB1743:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
addq $8, %rsp
.cfi_def_cfa_offset 8
jmp __cxa_atexit
.cfi_endproc
.LFE1743:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .text.unlikely
.LCOLDE2:
.section .text.startup
.LHOTE2:
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits


Test_2 Assembly



.file "main.cpp"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Time: "
.section .text.unlikely,"ax",@progbits
.LCOLDB1:
.section .text.startup,"ax",@progbits
.LHOTB1:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB1578:
.cfi_startproc
pushq %r12
.cfi_def_cfa_offset 16
.cfi_offset 12, -16
pushq %rbp
.cfi_def_cfa_offset 24
.cfi_offset 6, -24
movl $4, %edi
pushq %rbx
.cfi_def_cfa_offset 32
.cfi_offset 3, -32
movl $100000000, %ebx
call _Znwm
movq %rax, %rbp
call _ZNSt6chrono3_V212system_clock3nowEv
movq %rax, %r12
.p2align 4,,10
.p2align 3
.L2:
call rand
addl %eax, 0(%rbp)
subl $1, %ebx
jne .L2
call _ZNSt6chrono3_V212system_clock3nowEv
movl 0(%rbp), %esi
movl $_ZSt4cout, %edi
movq %rax, %rbx
call _ZNSolsEi
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movq %rbx, %rcx
movabsq $4835703278458516699, %rdx
movl $_ZSt4cout, %edi
subq %r12, %rcx
movq %rcx, %rax
sarq $63, %rcx
imulq %rdx
sarq $18, %rdx
movq %rdx, %rsi
subq %rcx, %rsi
call _ZNSo9_M_insertIlEERSoT_
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
popq %rbx
.cfi_def_cfa_offset 24
xorl %eax, %eax
popq %rbp
.cfi_def_cfa_offset 16
popq %r12
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1578:
.size main, .-main
.section .text.unlikely
.LCOLDE1:
.section .text.startup
.LHOTE1:
.section .text.unlikely
.LCOLDB2:
.section .text.startup
.LHOTB2:
.p2align 4,,15
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB1743:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
addq $8, %rsp
.cfi_def_cfa_offset 8
jmp __cxa_atexit
.cfi_endproc
.LFE1743:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .text.unlikely
.LCOLDE2:
.section .text.startup
.LHOTE2:
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits

Aucun commentaire:

Enregistrer un commentaire