Skip to content

Instantly share code, notes, and snippets.

@shinkbr
Last active October 22, 2019 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinkbr/b5cb623867f996da27c0403ef335d592 to your computer and use it in GitHub Desktop.
Save shinkbr/b5cb623867f996da27c0403ef335d592 to your computer and use it in GitHub Desktop.
$ time ./a.out
4./a.out  18.75s user 0.01s system 99% cpu 18.879 total

$ time ./b.out
4./b.out  23.44s user 0.02s system 99% cpu 23.550 total

$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

a.s, b.sはそれぞれ関数fに関する抜粋。

#include <stdio.h>
#define LOOP 1000000000
int main(int argc, char *argv[]){
unsigned int i;
int x;
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 1, 1);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 1, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 0, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 0, 0, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(0, 0, 0, 0);
}
printf("%d", x);
return 0;
}
int f(int a, int b, int c, int d) {
int m;
if (a == 1) {
if (b == 1) {
if (c == 1) {
if (d == 1) {
m = g();
} else {
m = 1;
}
} else {
m = 2;
}
} else {
m = 3;
}
} else {
m = 4;
}
return m;
}
int g() {
return 0;
}
.globl _f ## -- Begin function f
.p2align 4, 0x90
_f: ## @f
Lfunc_begin1:
.loc 1 15 0 ## a.c:15:0
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $32, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl %edx, -12(%rbp)
movl %ecx, -16(%rbp)
Ltmp7:
.loc 1 18 9 prologue_end ## a.c:18:9
cmpl $1, -4(%rbp)
Ltmp8:
.loc 1 18 7 is_stmt 0 ## a.c:18:7
jne LBB1_11
## %bb.1:
Ltmp9:
.loc 1 19 11 is_stmt 1 ## a.c:19:11
cmpl $1, -8(%rbp)
Ltmp10:
.loc 1 19 9 is_stmt 0 ## a.c:19:9
jne LBB1_9
## %bb.2:
Ltmp11:
.loc 1 20 13 is_stmt 1 ## a.c:20:13
cmpl $1, -12(%rbp)
Ltmp12:
.loc 1 20 11 is_stmt 0 ## a.c:20:11
jne LBB1_7
## %bb.3:
Ltmp13:
.loc 1 21 15 is_stmt 1 ## a.c:21:15
cmpl $1, -16(%rbp)
Ltmp14:
.loc 1 21 13 is_stmt 0 ## a.c:21:13
jne LBB1_5
## %bb.4:
Ltmp15:
.loc 1 22 15 is_stmt 1 ## a.c:22:15
callq _g
.loc 1 22 13 is_stmt 0 ## a.c:22:13
movl %eax, -20(%rbp)
.loc 1 23 9 is_stmt 1 ## a.c:23:9
jmp LBB1_6
Ltmp16:
LBB1_5:
.loc 1 24 13 ## a.c:24:13
movl $1, -20(%rbp)
Ltmp17:
LBB1_6:
.loc 1 26 7 ## a.c:26:7
jmp LBB1_8
Ltmp18:
LBB1_7:
.loc 1 27 11 ## a.c:27:11
movl $2, -20(%rbp)
Ltmp19:
LBB1_8:
.loc 1 29 5 ## a.c:29:5
jmp LBB1_10
Ltmp20:
LBB1_9:
.loc 1 30 9 ## a.c:30:9
movl $3, -20(%rbp)
Ltmp21:
LBB1_10:
.loc 1 32 3 ## a.c:32:3
jmp LBB1_12
Ltmp22:
LBB1_11:
.loc 1 33 7 ## a.c:33:7
movl $4, -20(%rbp)
Ltmp23:
LBB1_12:
.loc 1 36 10 ## a.c:36:10
movl -20(%rbp), %eax
.loc 1 36 3 is_stmt 0 ## a.c:36:3
addq $32, %rsp
popq %rbp
retq
Ltmp24:
Lfunc_end1:
.cfi_endproc
## -- End function
#include <stdio.h>
#define LOOP 1000000000
int main(int argc, char *argv[]){
unsigned int i;
int x;
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 1, 1);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 1, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 1, 0, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(1, 0, 0, 0);
}
for (i = 0; i < LOOP; i++) {
x = f(0, 0, 0, 0);
}
printf("%d", x);
return 0;
}
int f(int a, int b, int c, int d) {
int m;
if (a != 1) {
m = 4;
} else if (b != 1) {
m = 3;
} else if (c != 1) {
m = 2;
} else if (d != 1) {
m = 1;
} else {
m = g();
}
return m;
}
int g() {
return 0;
}
.globl _f ## -- Begin function f
.p2align 4, 0x90
_f: ## @f
Lfunc_begin1:
.loc 1 15 0 ## b.c:15:0
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $32, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl %edx, -12(%rbp)
movl %ecx, -16(%rbp)
Ltmp7:
.loc 1 18 9 prologue_end ## b.c:18:9
cmpl $1, -4(%rbp)
Ltmp8:
.loc 1 18 7 is_stmt 0 ## b.c:18:7
je LBB1_2
## %bb.1:
Ltmp9:
.loc 1 19 7 is_stmt 1 ## b.c:19:7
movl $4, -20(%rbp)
.loc 1 20 3 ## b.c:20:3
jmp LBB1_12
Ltmp10:
LBB1_2:
.loc 1 20 16 is_stmt 0 ## b.c:20:16
cmpl $1, -8(%rbp)
Ltmp11:
.loc 1 20 14 ## b.c:20:14
je LBB1_4
## %bb.3:
Ltmp12:
.loc 1 21 7 is_stmt 1 ## b.c:21:7
movl $3, -20(%rbp)
.loc 1 22 3 ## b.c:22:3
jmp LBB1_11
Ltmp13:
LBB1_4:
.loc 1 22 16 is_stmt 0 ## b.c:22:16
cmpl $1, -12(%rbp)
Ltmp14:
.loc 1 22 14 ## b.c:22:14
je LBB1_6
## %bb.5:
Ltmp15:
.loc 1 23 7 is_stmt 1 ## b.c:23:7
movl $2, -20(%rbp)
.loc 1 24 3 ## b.c:24:3
jmp LBB1_10
Ltmp16:
LBB1_6:
.loc 1 24 16 is_stmt 0 ## b.c:24:16
cmpl $1, -16(%rbp)
Ltmp17:
.loc 1 24 14 ## b.c:24:14
je LBB1_8
## %bb.7:
Ltmp18:
.loc 1 25 7 is_stmt 1 ## b.c:25:7
movl $1, -20(%rbp)
.loc 1 26 3 ## b.c:26:3
jmp LBB1_9
Ltmp19:
LBB1_8:
.loc 1 27 9 ## b.c:27:9
callq _g
.loc 1 27 7 is_stmt 0 ## b.c:27:7
movl %eax, -20(%rbp)
Ltmp20:
LBB1_9:
.loc 1 0 7 ## b.c:0:7
jmp LBB1_10
LBB1_10:
jmp LBB1_11
LBB1_11:
jmp LBB1_12
LBB1_12:
.loc 1 30 10 is_stmt 1 ## b.c:30:10
movl -20(%rbp), %eax
.loc 1 30 3 is_stmt 0 ## b.c:30:3
addq $32, %rsp
popq %rbp
retq
Ltmp21:
Lfunc_end1:
.cfi_endproc
## -- End function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment