2009年9月10日 星期四

研讀"About GCC printf optimization"by Peter Seiderer後的心得

研讀"About GCC printf optimization"by Peter Seiderer後,了解到不一樣string內容, gcc對printf有做不一樣的組譯動作,若要讓gcc將printf組譯成printf須在compile時加入"-fno-builtin-printf "參數,不然有可能會組譯最佳化的puts

qustion@jeff-debian:~/Samsung/hello/printf$ cat -b ./hello.c
1 #include
2 int main(int argc, char *argv[]) {
3 int i=0;
4 for(i=0;i<10000000;i++){
5 printf("hello world\n");
6 }
7 return 0;
8 }
qustion@jeff-debian:~/Samsung/hello/printf$ gcc -O0 -o hello hello.c
qustion@jeff-debian:~/Samsung/hello/printf$ nm --undefined-only ./hello
w _Jv_RegisterClasses
w __gmon_start__
U __libc_start_main@@GLIBC_2.0
U puts@@GLIBC_2.0
qustion@jeff-debian:~/Samsung/hello/printf$ time ./hello > /dev/null
real 0m0.699s
user 0m0.692s
sys 0m0.004s
qustion@jeff-debian:~/Samsung/hello/printf$
qustion@jeff-debian:~/Samsung/hello/printf$ gcc -O0 -fno-builtin-printf -o hello hello.c
qustion@jeff-debian:~/Samsung/hello/printf$ nm --undefined-only ./hello
w _Jv_RegisterClasses
w __gmon_start__
U __libc_start_main@@GLIBC_2.0
U printf@@GLIBC_2.0
qustion@jeff-debian:~/Samsung/hello/printf$ time ./hello > /dev/null
real 0m0.890s
user 0m0.880s
sys 0m0.008s
qustion@jeff-debian:~/Samsung/hello/printf$

reference:http://www.ciselant.de/projects/gcc_printf/gcc_printf.html

沒有留言: