evan

For the lasting love

导航

多线程 or 多进程 (实验1)

转自 http://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.
  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.

User+Sys will tell you how much actual CPU time your process used.

转自 http://guishan.org/use-time-command-to-get-linux-command-execution-time-and-stopwatch/

需要注意的是,real并不等于user+sys的总和。real代表的是程序从开始到结束的全部时间,即使程序不占CPU也统计时间。而user+sys是程序占用CPU的总时间,因此real总是大于或者等于user+sys的。

time这个命令可以用来统计一个程序的执行时间,并且能详细到用户态执行时间和内核态执行时间。

实验环境:http://www.cnblogs.com/eavn/archive/2010/09/08/1821774.html

实验数据:

255\100
255\300
255\500
255\800
thread process thread process thread process thread process
real0m0.784s real0m0.782s real0m2.653s real0m2.537s real0m4.397s real0m4.609s real0m6.795s real0m6.770s
user0m0.004s user0m0.024s user0m0.000s user0m0.072s user0m0.000s user0m0.144s user0m0.000s user0m0.196s
sys0m0.008s sys0m0.140s sys0m0.012s sys0m0.204s sys0m0.012s sys0m0.472s sys0m0.000s sys0m0.484s








real0m0.780s real0m0.775s real0m2.516s real0m2.491s real0m4.252s real0m4.311s real0m6.921s real0m6.993s
user0m0.000s user0m0.032s user0m0.004s user0m0.068s user0m0.004s user0m0.128s user0m0.000s user0m0.180s
sys0m0.008s sys0m0.116s sys0m0.004s sys0m0.232s sys0m0.008s sys0m0.408s sys0m0.008s sys0m0.536s








real0m0.784s real0m0.784s real0m2.526s real0m2.505s real0m4.269s real0m4.356s real0m7.159s real0m6.862s
user0m0.000s user0m0.036s user0m0.000s user0m0.048s user0m0.000s user0m0.148s user0m0.004s user0m0.180s
sys0m0.012s sys0m0.120s sys0m0.004s sys0m0.228s sys0m0.012s sys0m0.376s sys0m0.004s sys0m0.464s








real0m0.757s real0m0.783s real0m2.495s real0m2.504s real0m4.253s real0m4.253s real0m6.895s real0m6.906s
user0m0.000s user0m0.028s user0m0.004s user0m0.068s user0m0.000s user0m0.136s user0m0.004s user0m0.168s
sys0m0.012s sys0m0.132s sys0m0.012s sys0m0.224s sys0m0.012s sys0m0.428s sys0m0.008s sys0m0.548s








real0m0.793s real0m0.792s real0m2.495s real0m2.579s real0m4.182s real0m4.246s real0m6.939s real0m6.964s
user0m0.000s user0m0.036s user0m0.004s user0m0.108s user0m0.004s user0m0.160s user0m0.000s user0m0.208s
sys0m0.020s sys0m0.112s sys0m0.012s sys0m0.224s sys0m0.004s sys0m0.408s sys0m0.004s sys0m0.536s








real0m0.780s real0m0.793s real0m2.539s real0m2.532s real0m4.321s real0m4.223s real0m6.844s real0m6.940s
user0m0.004s user0m0.052s user0m0.000s user0m0.052s user0m0.000s user0m0.140s user0m0.000s user0m0.180s
sys0m0.004s sys0m0.144s sys0m0.008s sys0m0.256s sys0m0.004s sys0m0.392s sys0m0.008s sys0m0.464s








real0m0.807s real0m0.777s real0m2.517s real0m2.484s real0m4.182s real0m4.209s real0m7.196s real0m6.932s
user0m0.000s user0m0.036s user0m0.004s user0m0.080s user0m0.004s user0m0.128s user0m0.000s user0m0.172s
sys0m0.008s sys0m0.112s sys0m0.004s sys0m0.196s sys0m0.004s sys0m0.372s sys0m0.004s sys0m0.512s








real0m0.788s real0m0.797s real0m2.484s real0m2.485s real0m4.241s real0m5.371s real0m6.804s real0m6.961s
user0m0.000s user0m0.024s user0m0.000s user0m0.092s user0m0.000s user0m0.140s user0m0.000s user0m0.208s
sys0m0.012s sys0m0.136s sys0m0.004s sys0m0.260s sys0m0.008s sys0m0.400s sys0m0.012s sys0m0.480s








784.833333 785.166667 2514.333333 2508.5 4252.5 4332.333333 6926.666667 6927.333333
0.666667 31.5 2 71.5 1.333333 139 0.666667 185.166667
10 125.833333 7.333333 227.833333 8 401.5 6 501.666667

255\1000
255\5000
255\5000
thread process thread process thread process
real0m8.496s real0m8.961s real0m50.289s real0m51.965s real1m30.752s real1m31.121s
user0m0.008s user0m0.240s user0m1.704s user0m1.408s user0m2.052s user0m2.568s
sys0m0.008s sys0m0.804s sys0m2.936s sys0m3.392s sys0m8.477s sys0m6.216s






real0m8.460s real0m8.578s real0m44.868s real0m44.689s real1m30.542s real1m30.769s
user0m0.000s user0m0.336s user0m1.636s user0m1.316s user0m2.132s user0m2.532s
sys0m0.012s sys0m0.664s sys0m3.104s sys0m3.232s sys0m8.581s sys0m6.344s






real0m8.493s real0m8.450s real0m44.490s real0m45.209s real1m30.483s real1m31.582s
user0m0.004s user0m0.304s user0m0.992s user0m1.412s user0m2.000s user0m2.420s
sys0m0.012s sys0m0.740s sys0m2.060s sys0m3.600s sys0m8.097s sys0m6.112s






real0m8.442s real0m8.420s real0m43.916s real0m48.919s real1m31.136s real1m30.916s
user0m0.004s user0m0.268s user0m1.200s user0m1.332s user0m2.460s user0m2.520s
sys0m0.008s sys0m0.724s sys0m2.408s sys0m3.512s sys0m8.073s sys0m6.532s






real0m8.575s real0m8.450s real0m45.639s real0m47.608s real1m30.305s real1m31.100s
user0m0.000s user0m0.268s user0m1.232s user0m1.384s user0m1.500s user0m2.460s
sys0m0.008s sys0m0.740s sys0m2.156s sys0m3.468s sys0m6.564s sys0m6.256s






real0m8.533s real0m8.469s real0m43.383s real0m46.837s real1m13.056s real1m31.788s
user0m0.000s user0m0.292s user0m1.572s user0m1.284s user0m1.620s user0m2.648s
sys0m0.012s sys0m0.740s sys0m2.952s sys0m3.464s sys0m6.076s sys0m6.516s






real0m8.430s real0m8.427s real0m42.469s real0m55.889s real1m18.466s real1m31.126s
user0m0.004s user0m0.272s user0m1.544s user0m1.324s user0m1.228s user0m2.320s
sys0m0.008s sys0m0.756s sys0m2.944s sys0m3.448s sys0m5.712s sys0m5.596s






real0m8.642s real0m8.461s real0m44.512s real1m0.179s real1m18.096s real1m30.226s
user0m0.004s user0m0.228s user0m1.644s user0m1.224s user0m1.416s user0m2.272s
sys0m0.008s sys0m0.780s sys0m3.140s sys0m3.580s sys0m5.812s sys0m5.752s






8499.5 8471.666667 44467.833333 49404.166667 86440.333333 91102
2.666667 273.5 1470.666667 1341 1786.5 2469.5
9.333333 746 2749.333333 3476.666667 7182.833333 6199.166667

 


thread process
255\100 784.833333 785.166667

0.666667 31.5

10 125.833333
255\300 2514.333333 2508.5

2 71.5

7.333333 227.833333
255\500 4252.5 4332.333333

1.333333 139

8 401.5
255\800 6926.666667 6927.333333

0.666667 185.166667

6 501.666667
255\1000 8499.5 8471.666667

2.666667 273.5

9.333333 746
255\5000 44467.833333 49404.166667

1470.666667 1341

2749.333333 3476.666667
255\5000 86440.333333 91102

1786.5 2469.5

7182.833333 6199.166667

上述实验数据第一行为P_NUMBER\COUNT的值,每组实验做8次,然后去掉最高值、最低值,再取平均值,为最后三行

从平均值可看出real数据相差不大,相差较大的是user和sys,而根据前文描述用户关心的为real,因此相差不是很大,对linux系统的线程进程实现了解不是很深入,因此对其中的原因不是很清楚,望大牛给予指点

实验中最好是一个实验一个实验慢慢跑,如果是几个实验一起跑可能会有重大偏差

对于给的试验结果不太符合:任务量较大时,多进程比多线程效率高;而完成的任务量较小时,多线程比多进程要快,重复打印 600 次时,多进程与多线程所耗费的时间相同

而且可以说我的结论恰恰相反:任务量较小时,多进程效率较好,任务量较小时,多线程效率较好,这个分界点暂时没法确定

这个原因可能是因为系统不一样;我的求平均值假设不对;。。。

 

附件求平均值程序

输入data

代码
real 0m6.770s
user 0m0.196s
sys 0m0.484s

real 0m6.993s
user 0m0.180s
sys 0m0.536s

real 0m6.862s
user 0m0.180s
sys 0m0.464s

real 0m6.906s
user 0m0.168s
sys 0m0.548s

real 0m6.964s
user 0m0.208s
sys 0m0.536s

real 0m6.940s
user 0m0.180s
sys 0m0.464s

real 0m6.932s
user 0m0.172s
sys 0m0.512s

real 0m6.961s
user 0m0.208s
sys 0m0.480s
程序
代码
#include <stdlib.h>
#include
<stdio.h>
#include
<string.h>

#define MAXSZSIZE 32
#define MAXNUM 64

static int cmp(const void *p1, const void *p2)
{
return *((long*)p1) > *((long*)p2);
}

int main()
{
char sz_input[MAXSZSIZE];
char sz_filename[MAXSZSIZE]="data";

long l_real[MAXNUM],l_user[MAXNUM],l_sys[MAXNUM];
long *lp=NULL;

long long_min=0;
double df_second=0;

long l_real_total=0,l_user_total=0,l_sys_total=0;
double df_real_avg=0,df_user_avg=0,df_sys_avg=0;

FILE
*f_handle=NULL;
int int_flag=0,int_index=0;

memset (l_real,
0 ,sizeof(l_real));
memset (l_user,
0 ,sizeof(l_user));
memset (l_sys,
0 ,sizeof(l_sys));

f_handle
=fopen (sz_filename,"r");

if (f_handle==NULL)
{
perror(
"can not open file:");
return (1);
}

while (fscanf (f_handle,"%s",sz_input) !=EOF )
{
if (strcmp(sz_input,"real")==0)
{
lp
= l_real;
}
else if (strcmp(sz_input,"user")==0)
{
lp
= l_user;
}
else if (strcmp(sz_input,"sys")==0)
{
lp
= l_sys;
}
else
{
continue;
}

fscanf (f_handle,
"%ld%c%lf%c",&long_min,&sz_input[0],&df_second,&sz_input[1]);

//printf ("%ld %c %f %c\n", long_min, sz_input[0], df_second, sz_input[1]);
//exit (1);

*lp = (*lp) + 1;


*(lp+*lp) = (long) ((long_min*60+df_second)*1000);
}

for (int_index=1; int_index<=l_real[0]; int_index++)
printf (
"%ld\t%ld\t%ld\n",l_real[int_index],l_user[int_index],l_sys[int_index]);

puts(
"\n");
qsort(l_real
+1,*l_real,sizeof(long),cmp);
qsort(l_user
+1,*l_user,sizeof(long),cmp);
qsort(l_sys
+1,*l_sys,sizeof(long),cmp);

for (int_index=1; int_index<=l_real[0]; int_index++)
printf (
"%ld\t%ld\t%ld\n",l_real[int_index],l_user[int_index],l_sys[int_index]);

puts(
"\n");
l_real_total
= 0;
l_user_total
= 0;
l_sys_total
= 0;
for (int_index=2; int_index<l_real[0]; int_index++)
{
l_real_total
+= l_real[int_index];
l_user_total
+= l_user[int_index];
l_sys_total
+= l_sys[int_index];
}

df_real_avg
= l_real_total*1.0 / (l_real[0]-2);
df_user_avg
= l_user_total*1.0 / (l_user[0]-2);
df_sys_avg
= l_sys_total*1.0 / (l_sys[0]-2);

printf (
"%lf\n%lf\n%lf\n", df_real_avg,df_user_avg,df_sys_avg);

return 0;
}

 



posted on 2010-09-10 19:05  evander  阅读(480)  评论(0编辑  收藏  举报