Linux中的system和popen的效率比較

2021-07-01 18:45:40 字數 702 閱讀 4287

在本次專案中有比較多的直接調命令的地方,所以就在想用system函式還是popen函式,哪個效率更高呢。在網上找了找,看見說什麼的都有,而且感覺他們的測試方法也有點不太贊同,所以就自己寫了個測試**,看看到底哪個效率更高點。

關於system函式和popen函式的基本知識在這就不再提了,只發表一下自己的測試方法和測試結果:

平台:ubuntu release 7.04

linux 核心:2.6.20

cpu:雙核 i3

gcc:4.1.2

**:

int main(int argc, char *argv)

end = clock();

t = (double)(end - start) / clocks_per_sec;

printf("%f seconds is used\n", t);

return 0;

}測試結果:

迴圈1000次:

system:

0.040000 seconds is used

popen:

0.040000 seconds is used

迴圈10000次:

system:

0.340000 seconds is used

popen:

0.460000 seconds is used

結論:system的效率更高

linux中system和exec函式的區別

system 是用shell來呼叫程式 fork exec waitpid,而exec是直接讓你的程式代替用來的程式執行。system 是在單獨的程序中執行命令,完了還會回到你的程式中。而exec函式是直接在你的程序中執行新的程式,新的程式會把你的程式覆蓋,除非呼叫出錯,否則你再也回不到exec後面...

LINUX中system()函式詳解

相關函式 fork,execve,waitpid,popen 表頭檔案 include 定義函式 int system const char string 函式說明 system 會呼叫fork 產生子程序,由子程序來呼叫 bin sh c string來執行引數string字串所代表的命令,此命 ...

Linux中的system()函式工作原理

一 linux中的system 函式源 include include include include int system const char cmdstring if pid fork 0 else if pid 0 else return status 當system接受的命令為null時直...