日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 进程的执行时间,Linux 获取进程执行时间

發布時間:2023/12/1 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 进程的执行时间,Linux 获取进程执行时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux 獲取進程執行時間

1 前言

測試一個程序的執行時間, 時間包括用戶 CPU 時間系統 CPU 時間時鐘時間之前獲取之前時間都是在程序的 main 函數用 time 函數實現, 這個只能粗略的計算程序的執行時間, 不能準確的獲取其他時間在看 APUE 時, 書中有關程序時間測試程序, 非常正規, 提供這三個時間如是, 上網搜了一下, 進行總結一下

2 獲取方法

有兩種方法可以獲取, 第一種是用 time 命令, time 進程第二種是通過在程序中進行記錄, 首先利用 sysconf 函數獲取時鐘滴答數, 再用 times 獲取 tms 結構

查看 times 函數, man 2 tms, 得到 tms 結構定義和 times 函數聲明如下:structtms{

clock_ttms_utime;/* user time */

clock_ttms_stime;/* system time */

clock_ttms_cutime;/* user time of children */

clock_ttms_cstime;/* system time of children */

};#include

clock_ttimes(structtms*buf);

注意: 此處計算的時間是時鐘滴答數, 需要除以系統時鐘滴答數, 得出實際的秒數

3 測試例子:

測試程序如下:#include

#include

#include

#include

#defineBUFFER_SIZE4*1024

intmain()

{

intsc_clk_tck;

sc_clk_tck=sysconf(_SC_CLK_TCK);

structtms begin_tms,end_tms;

clock_tbegin,end;

system("date");

begin=times(&begin_tms);

sleep(2);

end=times(&end_tms);

printf("real time: %lf\n",(end-begin)/(double)sc_clk_tck);

printf("user time: %lf\n",

(end_tms.tms_utime-begin_tms.tms_utime)/(double)sc_clk_tck);

printf("sys time: %lf\n",

(end_tms.tms_stime-begin_tms.tms_stime)/(double)sc_clk_tck);

printf("child user time: %lf\n",

(end_tms.tms_cutime-begin_tms.tms_cutime)/(double)sc_clk_tck);

printf("child sys time: %lf\n",

(end_tms.tms_cstime-begin_tms.tms_cstime)/(double)sc_clk_tck);

return0;

}

測試結果如下所示:

采用 time 命令, 測試結果如下所示:

4 參考網址http://www.01happy.com/linux-process-time/

http://www.01happy.com/c-get-process-time/

linux 查看進程的時鐘時間用戶 CPU 時間和系統 CPU 時間

在 linux 下進行編程時, 可能會涉及度量進程的執行時間 linux 下進程的時間值分三種:

時鐘時間(real time): 指進程從開始執行到結束, 實際執行的時間

用戶 CPU 時間(user CPU time): 指進程中執行用戶指令所用的時間, 也包括子進程

系統 CPU 時間(system CPU time): 指為進程執行內核程序所經歷的時間, 例如調用 read 和 write 內核方法時, 消耗的時間就計入系統 CPU 時間

在 linux 下, 可以使用 time 命令來查看程序執行時這三種時間值的消耗筆者寫了一個測試程序, 來演示這一個過程:#include

intmain(void)

{

inti;

while(i<=10E7){

i++;

}

return1;

}

程序非常簡單了, 就不說明了, 編譯成二進制文件 a.out, 使用 time 命令執行, 在筆者的電腦上輸入如下信息:$ time./a.out

real0m0.349s

user0m0.340s

sys0m0.004s

其中 real 表示時鐘時間, user 表示用戶 CPU 時間, sys 表示系統 CPU 時間 time 命令也可以用于系統的命令, 如 time lstime ps 等等

C 語言獲取時鐘時間用戶 CPU 時間和系統 CPU 時間

C 語言里可以通過 times 函數獲取這三種時間, times 函數說明如下:#include

clock_ttimes(structtms*buf);

參數 tms 的結構如下:structtms{

clock_ttms_utime;/* user time */

clock_ttms_stime;/* system time */

clock_ttms_cutime;

/* user time of children */

clock_ttms_cstime;

/* system time of children */

};

其中時間都是以滴答數 (clock tick) 為單位, 詳細可以用 man 2 times 查看幫助手冊下面的示例用來計算執行系統命令 date 消耗的三種時間值#include

#include

#include

#include

intmain(void)

{

// 獲取滴答數, 在 ubuntu 12.04 下為 100

intclktck=0;

if

((clktck=sysconf(_SC_CLK_TCK))<0){

printf("%s\n","sysconf error");

exit(0);

}

struct

tms? tmsstart,tmsend;

clock_tstart,end;

// 獲取開始時間

if

((start=times(&tmsstart))==-1){

printf("%s\n","times error");

exit(0);

}

// 執行系統函數 date

system("date");

// 獲取結束時間

if

((end=times(&tmsend))==-1){

printf("%s\n","times error");

exit(0);

}

printf("real: %7.2f\n",(end-start)/(double)clktck);

printf("user: %7.2f\n",

(tmsend.tms_utime-tmsstart.tms_utime)/(

double)clktck);

printf("sys:? %7.2f\n",

(tmsend.tms_stime-tmsstart.tms_stime)/(

double)clktck);

printf(

"child user: %7.2f\n"

,

(tmsend.tms_cutime-tmsstart.tms_cutime)/(

double)clktck);

printf(

"child sys:? %7.2f\n"

,

(tmsend.tms_cstime-tmsstart.tms_cstime)/(

double)clktck);

return1;

}

編譯執行上面的程序, 輸出如下:$./a.out

SunDec912:50:39CST2012

real:0.01

user:0.00

sys:0.00

child user:0.00

child sys:0.00

其中 child user 就是執行 date 命令消耗的用戶 CPU 時間, child sys 就是執行 date 命令消耗的系統 CPU 時間這里會發現這兩個值都為 0, 因為滴答數為 100, 只能精確到小數點后面兩位, date 的執行時間非常快, 所以就為 0 了如何精確到小數點后面 3 位呢?

來源: http://www.bubuko.com/infodetail-2506242.html

總結

以上是生活随笔為你收集整理的linux 进程的执行时间,Linux 获取进程执行时间的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。