多线程 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
threadprocessthreadprocessthreadprocessthreadprocess
real0m0.784sreal0m0.782sreal0m2.653sreal0m2.537sreal0m4.397sreal0m4.609sreal0m6.795sreal0m6.770s
user0m0.004suser0m0.024suser0m0.000suser0m0.072suser0m0.000suser0m0.144suser0m0.000suser0m0.196s
sys0m0.008ssys0m0.140ssys0m0.012ssys0m0.204ssys0m0.012ssys0m0.472ssys0m0.000ssys0m0.484s








real0m0.780sreal0m0.775sreal0m2.516sreal0m2.491sreal0m4.252sreal0m4.311sreal0m6.921sreal0m6.993s
user0m0.000suser0m0.032suser0m0.004suser0m0.068suser0m0.004suser0m0.128suser0m0.000suser0m0.180s
sys0m0.008ssys0m0.116ssys0m0.004ssys0m0.232ssys0m0.008ssys0m0.408ssys0m0.008ssys0m0.536s








real0m0.784sreal0m0.784sreal0m2.526sreal0m2.505sreal0m4.269sreal0m4.356sreal0m7.159sreal0m6.862s
user0m0.000suser0m0.036suser0m0.000suser0m0.048suser0m0.000suser0m0.148suser0m0.004suser0m0.180s
sys0m0.012ssys0m0.120ssys0m0.004ssys0m0.228ssys0m0.012ssys0m0.376ssys0m0.004ssys0m0.464s








real0m0.757sreal0m0.783sreal0m2.495sreal0m2.504sreal0m4.253sreal0m4.253sreal0m6.895sreal0m6.906s
user0m0.000suser0m0.028suser0m0.004suser0m0.068suser0m0.000suser0m0.136suser0m0.004suser0m0.168s
sys0m0.012ssys0m0.132ssys0m0.012ssys0m0.224ssys0m0.012ssys0m0.428ssys0m0.008ssys0m0.548s








real0m0.793sreal0m0.792sreal0m2.495sreal0m2.579sreal0m4.182sreal0m4.246sreal0m6.939sreal0m6.964s
user0m0.000suser0m0.036suser0m0.004suser0m0.108suser0m0.004suser0m0.160suser0m0.000suser0m0.208s
sys0m0.020ssys0m0.112ssys0m0.012ssys0m0.224ssys0m0.004ssys0m0.408ssys0m0.004ssys0m0.536s








real0m0.780sreal0m0.793sreal0m2.539sreal0m2.532sreal0m4.321sreal0m4.223sreal0m6.844sreal0m6.940s
user0m0.004suser0m0.052suser0m0.000suser0m0.052suser0m0.000suser0m0.140suser0m0.000suser0m0.180s
sys0m0.004ssys0m0.144ssys0m0.008ssys0m0.256ssys0m0.004ssys0m0.392ssys0m0.008ssys0m0.464s








real0m0.807sreal0m0.777sreal0m2.517sreal0m2.484sreal0m4.182sreal0m4.209sreal0m7.196sreal0m6.932s
user0m0.000suser0m0.036suser0m0.004suser0m0.080suser0m0.004suser0m0.128suser0m0.000suser0m0.172s
sys0m0.008ssys0m0.112ssys0m0.004ssys0m0.196ssys0m0.004ssys0m0.372ssys0m0.004ssys0m0.512s








real0m0.788sreal0m0.797sreal0m2.484sreal0m2.485sreal0m4.241sreal0m5.371sreal0m6.804sreal0m6.961s
user0m0.000suser0m0.024suser0m0.000suser0m0.092suser0m0.000suser0m0.140suser0m0.000suser0m0.208s
sys0m0.012ssys0m0.136ssys0m0.004ssys0m0.260ssys0m0.008ssys0m0.400ssys0m0.012ssys0m0.480s








784.833333785.1666672514.3333332508.54252.54332.3333336926.6666676927.333333
0.66666731.5271.51.3333331390.666667185.166667
10125.8333337.333333227.8333338401.56501.666667

255\1000
255\5000
255\5000
threadprocessthreadprocessthreadprocess
real0m8.496sreal0m8.961sreal0m50.289sreal0m51.965sreal1m30.752sreal1m31.121s
user0m0.008suser0m0.240suser0m1.704suser0m1.408suser0m2.052suser0m2.568s
sys0m0.008ssys0m0.804ssys0m2.936ssys0m3.392ssys0m8.477ssys0m6.216s






real0m8.460sreal0m8.578sreal0m44.868sreal0m44.689sreal1m30.542sreal1m30.769s
user0m0.000suser0m0.336suser0m1.636suser0m1.316suser0m2.132suser0m2.532s
sys0m0.012ssys0m0.664ssys0m3.104ssys0m3.232ssys0m8.581ssys0m6.344s






real0m8.493sreal0m8.450sreal0m44.490sreal0m45.209sreal1m30.483sreal1m31.582s
user0m0.004suser0m0.304suser0m0.992suser0m1.412suser0m2.000suser0m2.420s
sys0m0.012ssys0m0.740ssys0m2.060ssys0m3.600ssys0m8.097ssys0m6.112s






real0m8.442sreal0m8.420sreal0m43.916sreal0m48.919sreal1m31.136sreal1m30.916s
user0m0.004suser0m0.268suser0m1.200suser0m1.332suser0m2.460suser0m2.520s
sys0m0.008ssys0m0.724ssys0m2.408ssys0m3.512ssys0m8.073ssys0m6.532s






real0m8.575sreal0m8.450sreal0m45.639sreal0m47.608sreal1m30.305sreal1m31.100s
user0m0.000suser0m0.268suser0m1.232suser0m1.384suser0m1.500suser0m2.460s
sys0m0.008ssys0m0.740ssys0m2.156ssys0m3.468ssys0m6.564ssys0m6.256s






real0m8.533sreal0m8.469sreal0m43.383sreal0m46.837sreal1m13.056sreal1m31.788s
user0m0.000suser0m0.292suser0m1.572suser0m1.284suser0m1.620suser0m2.648s
sys0m0.012ssys0m0.740ssys0m2.952ssys0m3.464ssys0m6.076ssys0m6.516s






real0m8.430sreal0m8.427sreal0m42.469sreal0m55.889sreal1m18.466sreal1m31.126s
user0m0.004suser0m0.272suser0m1.544suser0m1.324suser0m1.228suser0m2.320s
sys0m0.008ssys0m0.756ssys0m2.944ssys0m3.448ssys0m5.712ssys0m5.596s






real0m8.642sreal0m8.461sreal0m44.512sreal1m0.179sreal1m18.096sreal1m30.226s
user0m0.004suser0m0.228suser0m1.644suser0m1.224suser0m1.416suser0m2.272s
sys0m0.008ssys0m0.780ssys0m3.140ssys0m3.580ssys0m5.812ssys0m5.752s






8499.58471.66666744467.83333349404.16666786440.33333391102
2.666667273.51470.66666713411786.52469.5
9.3333337462749.3333333476.6666677182.8333336199.166667

 


threadprocess
255\100784.833333785.166667

0.66666731.5

10125.833333
255\3002514.3333332508.5

271.5

7.333333227.833333
255\5004252.54332.333333

1.333333139

8401.5
255\8006926.6666676927.333333

0.666667185.166667

6501.666667
255\10008499.58471.666667

2.666667273.5

9.333333746
255\500044467.83333349404.166667

1470.6666671341

2749.3333333476.666667
255\500086440.33333391102

1786.52469.5

7182.8333336199.166667

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

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

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

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

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

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

 

附件求平均值程序

输入data

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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
程序
ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
#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 ;
}

 



转载于:https://www.cnblogs.com/eavn/archive/2010/09/10/1823545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值