1. 程式人生 > >獲得程序執行時間

獲得程序執行時間

Linux中有很多獲得時間資訊的方法,但各自有各自的優缺點

1.使用time命令

# time ./a.out ,,,//這是a.out 執行的內容

real    0m0.001s user    0m0.001s sys    0m0.001s

優點:可以直接獲得程序的各種時間

缺點:只能在命令列執行

2.使用wait3獲得程序的時間

相關結構體:

struct rusage {         struct timeval ru_utime;         struct timeval ru_stime;         long   ru_maxrss;                long   ru_ixrss;                 long   ru_idrss;                 long   ru_isrss;                 long   ru_minflt;                long   ru_majflt;                long   ru_nswap;                 long   ru_inblock;               long   ru_oublock;               long   ru_msgsnd;                long   ru_msgrcv;                long   ru_nsignals;              long   ru_nvcsw;                 long   ru_nivcsw;        };

    struct timeval   {     __time_t tv_sec;        /* Seconds.  */     __suseconds_t tv_usec;    /* Microseconds.  */   };

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>

int main(void)
{
	pid_t pid;
	struct rusage my_rusage;
	if ((pid = fork()) < 0) 
	{
		perror("fork error");
		exit(-1);
	} else if (pid == 0) 
	{ 		
		printf("子程序PID = %ld, \n", (long)getpid());
		exit(0);
	} else 
	{
		int *stat_loc = malloc(sizeof(int));
		wait3(stat_loc, 0, &my_rusage);
		printf("子程序執行時間為:\n");
		printf("秒   = %ld \n", (long)my_rusage.ru_utime.tv_sec);   //秒
		printf("微秒 = %ld \n", (long)my_rusage.ru_utime.tv_usec);  //微秒
		
		printf("父程序PID = %ld, \n", (long)getpid());
		free(stat_loc);
		exit(0);		
	}
}

執行結果:

# ./a.out  子程序PID = 11270,  子程序執行時間為: 秒   = 0  微秒 = 79  父程序PID = 11269,

優點:可以獲得很多程序的執行資訊,包括時間資訊

缺點:只能獲得子程序的資訊,還要等待子程序結束後,才能獲得

3.使用times函式獲得程序時間

相關結構體:

struct tms { clock_t tms_utime; /* user CPU time */ clock_t tms_stime; /* system CPU time */ clock_t tms_cutime; /* user CPU time, terminated children */ clock_t tms_cstime; /* system CPU time, terminated children */ };

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>
#define bufsize 20
static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(void);

int
main(int argc, char *argv[])
{
	int i;
	setbuf(stdout, NULL);
	do_cmd(); /* once for each command-line arg */
	exit(0);
}
static void
do_cmd(void) /* execute and time the "cmd" */
{
	struct tms tmsstart, tmsend;
	clock_t start, end;

	if ((start = times(&tmsstart)) == -1) /* starting values */
		perror("times error");
	sleep(2);  //delay two second
	if ((end = times(&tmsend)) == -1) /* ending values */
		perror("times error");
	pr_times(end-start, &tmsstart, &tmsend);
}
static void
pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
{
	static long clktck = 0;
	if (clktck == 0) /* fetch clock ticks per second first time */
	{
		if ((clktck = sysconf(_SC_CLK_TCK)) < 0)
			perror("sysconf error");
	}
		
	printf(" real: %7.2f\n", real / (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);
}

優點:可以獲得程序中任意時間段的時間資訊,

缺點:

1.不能獲取子程序的時間的時間資訊,

2.它只能獲取一段的資訊,而不能獲取整個程序的資訊,