コード:
#include <stdio.h>
#include <time.h>
// 構造体を利用。
struct st {
int no;
char name[13];
double average;
} s = {0};
char array[sizeof(int)+13+sizeof(double)];
void subA(int loops)
{
int i,c,d,l;
for( l=0 ; l<loops ; l++ ) {
i = s.no;
c = s.name[0];
d = s.average;
}
}
void subB(int loops)
{
int i,c,d,l;
// 配列を強引にアクセス
for( l=0 ; l<loops ; l++ ) {
i = *(int*)array;
c = array[sizeof(int)];
d = *(double*)(array + sizeof(int) + 13);
}
}
int main(){
int ms1,ms2;
struct timespec ts;
// 計測A
clock_gettime(CLOCK_REALTIME, &ts);
ms1 = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
subA(10000000);
clock_gettime(CLOCK_REALTIME, &ts);
ms2 = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
printf("time A: %dms\n", ms2-ms1);
// 計測B
clock_gettime(CLOCK_REALTIME, &ts);
ms1 = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
subB(10000000);
clock_gettime(CLOCK_REALTIME, &ts);
ms2 = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
printf("time B: %dms\n", ms2-ms1);
return 0;
}
コード:
.file "a.c"
.text
.p2align 4,,15
.globl _subA
.def _subA; .scl 2; .type 32; .endef
_subA:
pushl %ebp
movl %esp, %ebp
popl %ebp
ret
.p2align 4,,15
.globl _subB
.def _subB; .scl 2; .type 32; .endef
_subB:
pushl %ebp
movl %esp, %ebp
popl %ebp
ret
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "time A: %dms\12\0"
LC1:
.ascii "time B: %dms\12\0"
.text
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
subl $56, %esp
movl %edi, -4(%ebp)
leal -24(%ebp), %edi
movl %ecx, -16(%ebp)
movl %ebx, -12(%ebp)
movl %esi, -8(%ebp)
call ___main
movl %edi, 4(%esp)
movl $1, (%esp)
call _clock_gettime
movl -20(%ebp), %ebx
movl -24(%ebp), %esi
movl %edi, 4(%esp)
movl $1, (%esp)
call _clock_gettime
movl -20(%ebp), %eax
imull $1000, %esi, %esi
movl %eax, -36(%ebp)
movl $1125899907, %eax
imull -36(%ebp)
imull $1000, -24(%ebp), %eax
sarl $31, -36(%ebp)
sarl $18, %edx
subl -36(%ebp), %edx
movl $LC0, (%esp)
leal (%edx,%eax), %ecx
movl $1125899907, %eax
imull %ebx
sarl $31, %ebx
sarl $18, %edx
subl %ebx, %edx
subl %edx, %ecx
subl %esi, %ecx
movl %ecx, 4(%esp)
call _printf
movl %edi, 4(%esp)
movl $1, (%esp)
call _clock_gettime
movl -20(%ebp), %ebx
movl -24(%ebp), %esi
movl %edi, 4(%esp)
movl $1, (%esp)
call _clock_gettime
movl -20(%ebp), %edi
movl $1125899907, %eax
imull $1000, %esi, %esi
imull %edi
imull $1000, -24(%ebp), %eax
sarl $31, %edi
movl $LC1, (%esp)
sarl $18, %edx
subl %edi, %edx
leal (%edx,%eax), %ecx
movl $1125899907, %eax
imull %ebx
subl %esi, %ecx
sarl $31, %ebx
movl %edx, %esi
sarl $18, %esi
subl %ebx, %esi
subl %esi, %ecx
movl %ecx, 4(%esp)
call _printf
movl -16(%ebp), %ecx
xorl %eax, %eax
movl -12(%ebp), %ebx
movl -8(%ebp), %esi
movl -4(%ebp), %edi
movl %ebp, %esp
popl %ebp
leal -4(%ecx), %esp
ret
.globl _s
.bss
.align 32
_s:
.space 32
.comm _array, 25, 0
.def _clock_gettime; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef