檢視printf 執行效率

2021-10-05 13:36:57 字數 1469 閱讀 7329

1.編寫乙個測試檔案

#inciude #include int main()

return;

}

2.然後編譯執行

root@ubuntu:/home/code/shell/test# vi test2.c 

root@ubuntu:/home/code/shell/test# gcc -o test2 test2.c

root@ubuntu:/home/code/shell/test# ls

1.log 2.log test1.c test2 test2-1 test2.c test3 test.c test.sh vehicle

root@ubuntu:/home/code/shell/test# time ./test2

。。。行數太多忽略部分。。。

it is 99994

it is 99995

it is 99996

it is 99997

it is 99998

it is 99999

it is 100000

real 0m2.592s

user 0m0.016s

sys 0m0.252s

root@ubuntu:/home/code/shell/test#

real代表程式執行時間,user表示使用者時間,sys表示系統核心時間,我們看的是real就行,我們程式執行的時間是2.592s

3.把printf定向輸出到1.log檔案中

root@ubuntu:/home/code/shell/test# time ./test2 > 1.log

real 0m0.022s

user 0m0.020s

sys 0m0.004s

root@ubuntu:/home/code/shell/test#

現在程式執行時間是0.022s與上次大概快100倍,現在我們檢視下定向輸出到1.log檔案裡的內容

root@ubuntu:/home/code/shell/test# tail -f 1.log 

it is 99991

it is 99992

it is 99993

it is 99994

it is 99995

it is 99996

it is 99997

it is 99998

it is 99999

it is 100000

4.原因

prindf預設定向輸出到顯示屏上,但是顯示屏輸出有行緩衝,所以會造成時間延時,大部分時間是在等待行緩衝,造成程式效率低下,如果定向輸出到檔案中會提高效率。

5結論在實際工程編譯的時候把printf()遮蔽掉或者去掉,因為實際情況大部分是不需要輸出的,在程式除錯階段我們可以用printf().

mysql檢視SQL語句執行效率

主要用explain命令去檢視 語法 explain select from where 例如 explain select from city 結果如下 idselect type table type possible keys keykey len refrows extra 1 city a...

MySQL語句的執行效率檢視

explain顯示了mysql如何使用索引來處理select語句以及連線表。可以幫助選擇更好的索引和寫出更優化的查詢語句。使用方法,在select語句前加上explain就可以了 如 explain select surname,first name form a,b where a.id b.id...

sql server中如何檢視執行效率不高的語句

select creation time n 語句編譯時間 last execution time n 上次執行時間 total physical reads n 物理讀取總次數 total logical reads execution count n 每次邏輯讀次數 total logical ...