在WindowsXP的Mingw下通过写了两个测试时间和空间的小程序,if和switch的对比,然后通过gcc编译出.s和.exe文件去执行。
--------------------------------------------------------------------------------------
1. switch.c
#include "stdio.h"
#include "windows.h"
#define TOTAL_NUM (10)
#define TOTAL_TIME (0x4FFFFFFF)
int main(int argc, char **argv)
{
int value = 7;
int nGet = 0;
unsigned long start;
unsigned long finish;
int test_n;
start = GetTickCount();
printf("start:%ld/n", start);
for (test_n = 0; test_n < TOTAL_TIME; test_n++)
{
switch (value)
{
case 0:
default:
nGet = 0;
break;
case 1:
nGet = 1;
break;
case 2:
nGet = 2;
break;
case 3:
nGet = 3;
break;
case 4:
nGet = 4;
break;
case 5:
nGet = 5;
break;
case 6:
nGet = 6;
break;
case 7:
nGet = 7;
break;
case 8:
nGet = 8;
break;
case 9:
nGet = 9;
break;
}
}
printf("value:%d/n", nGet);
finish=GetTickCount()-start; //程序运行的时间
printf("time:%ld/n", finish);
return 0;
}
/* #include */
/* clock_t clock( void ); */
2. if.c
#include "stdio.h"
#include "windows.h"
#define TOTAL_NUM (10)
#define TOTAL_TIME (0x4FFFFFFF)
int main(int argc, char **argv)
{
int value = 7;
int nGet = 0;
int stable[TOTAL_NUM] = {0,1,2,3,4,5,6,7,8,9};
unsigned long start;
unsigned long finish;
int test_n;
start = GetTickCount();
printf("start:%ld/n", start);
for (test_n = 0; test_n < TOTAL_TIME; test_n++)
{
if ((value < 0) || (value >= TOTAL_NUM))
{
value = 0;
}
nGet = stable[value];
}
printf("value:%d/n", nGet);
finish=GetTickCount() - start; //程序运行的时间
printf("time:%ld/n", finish);
return 0;
}
/* #include */
/* clock_t clock( void ); */
--------------------------------------------------------------------------------------
然后
gcc -S switch.c
生成switch.s
.file "table_s.c"
.def ___main; .scl 2; .type 32; .endef
.text
LC0:
.ascii "start:%ld/12/0"
LC1:
.ascii "value:%d/12/0"
LC2:
.ascii "time:%ld/12/0"
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -24(%ebp)
movl -24(%ebp), %eax
call __alloca
call ___main
movl $7, -4(%ebp)
movl $0, -8(%ebp)
call _GetTickCount@0
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, -20(%ebp)
L32:
cmpl $1342177278, -20(%ebp)
jle L35
jmp L33
L35:
cmpl $9, -4(%ebp)
ja L38
movl -4(%ebp), %eax
sall $2, %eax
movl L48(%eax), %eax
jmp *%eax
.p2align 2
.align 4
L48:
.long L38
.long L39
.long L40
.long L41
.long L42
.long L43
.long L44
.long L45
.long L46
.long L47
L38:
movl $0, -8(%ebp)
jmp L34
L39:
movl $1, -8(%ebp)
jmp L34
L40:
movl $2, -8(%ebp)
jmp L34
L41:
movl $3, -8(%ebp)
jmp L34
L42:
movl $4, -8(%ebp)
jmp L34
L43:
movl $5, -8(%ebp)
jmp L34
L44:
movl $6, -8(%ebp)
jmp L34
L45:
movl $7, -8(%ebp)
jmp L34
L46:
movl $8, -8(%ebp)
jmp L34
L47:
movl $9, -8(%ebp)
L34:
leal -20(%ebp), %eax
incl (%eax)
jmp L32
L33:
movl -8(%ebp), %eax
movl %eax, 4(%esp)
movl $LC1, (%esp)
call _printf
call _GetTickCount@0
subl -12(%ebp), %eax
movl %eax, -16(%ebp)
movl -16(%ebp), %eax
movl %eax, 4(%esp)
movl $LC2, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _printf; .scl 2; .type 32; .endef
gcc -S if.c
生成if.s
.file "table_n.c"
.def ___main; .scl 2; .type 32; .endef
.text
LC0:
.ascii "start:%ld/12/0"
LC1:
.ascii "value:%d/12/0"
LC2:
.ascii "time:%ld/12/0"
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $104, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -88(%ebp)
movl -88(%ebp), %eax
call __alloca
call ___main
movl $7, -12(%ebp)
movl $0, -16(%ebp)
movl $0, -72(%ebp)
movl $1, -68(%ebp)
movl $2, -64(%ebp)
movl $3, -60(%ebp)
movl $4, -56(%ebp)
movl $5, -52(%ebp)
movl $6, -48(%ebp)
movl $7, -44(%ebp)
movl $8, -40(%ebp)
movl $9, -36(%ebp)
call _GetTickCount@0
movl %eax, -76(%ebp)
movl -76(%ebp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, -84(%ebp)
L32:
cmpl $1342177278, -84(%ebp)
jle L35
jmp L33
L35:
cmpl $0, -12(%ebp)
js L37
cmpl $9, -12(%ebp)
jg L37
jmp L36
L37:
movl $0, -12(%ebp)
L36:
movl -12(%ebp), %eax
movl -72(%ebp,%eax,4), %eax
movl %eax, -16(%ebp)
leal -84(%ebp), %eax
incl (%eax)
jmp L32
L33:
movl -16(%ebp), %eax
movl %eax, 4(%esp)
movl $LC1, (%esp)
call _printf
call _GetTickCount@0
subl -76(%ebp), %eax
movl %eax, -80(%ebp)
movl -80(%ebp), %eax
movl %eax, 4(%esp)
movl $LC2, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _printf; .scl 2; .type 32; .endef
可以对比一下,if生成出来的代码要少一些。
--------------------------------------------------------------------------------------
然后生成.exe
gcc -o switch if.c
if >i.txt
start:36931265
value:7
time:5235
gcc -o switch switch.c
switch >s.txt
start:36951500
value:7
time:5375
我们可以把
#define TOTAL_TIME (0x4FFFFFFF)
这个值继续加大,然后可以察看两个.c文件执行所用的时间。
你是否也喜欢用table和if结合去实现呢?