请问对于多线程程序应该如何测试线程的切换开销呢

hitytl 2008-06-23 04:36:49
我做了一个程序的两个版本,跑同样的20个数据,用线程实现(启动20个线程分别处理20个数据)需要用8秒;和用进程实现(启动20个进程分别处理20个数据)却只需要4秒,不是应该是线程的运行速度要高于进程么,怎么反了呢?我怀疑会不会是线程进行切换时会有很大的开销,应该如何测试线程切换的开销呢,另外多进程环境下应该不会有进程切换的开销吧。谢谢
...全文
716 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hitytl 2008-06-25
  • 打赏
  • 举报
回复
我想问下wooyz,你有intel的vtune你有linux下的么,最好是可破解的,我从EMULE下了个9.0的,但是提示
Invalid Licence。谢谢
fuqd273 2008-06-24
  • 打赏
  • 举报
回复
如果只有一个线程使用了pthread_mutex_t mutex
那么就删掉吧

只有一个线程使用和都不使用是没区别的。

只有超过一个线程使用pthread_mutex_t mutex,这个pthread_mutex_t mutex才有作用。
fierygnu 2008-06-24
  • 打赏
  • 举报
回复
wall clock time就是日常的时间,gettimeofday返回值的差就是这个值,在这个时间内,系统可以调度执行很多的进程(线程),每个进程的执行时间都不是这个时间差。要想知道每个进程的执行时间,要用time(),它统计的才是进程真正的执行时间。
hitytl 2008-06-24
  • 打赏
  • 举报
回复
只有一个负责线程管理的线程中使用了pthread_mutex_t mutex的lock与unlock,普通线程体中没有使用互斥操作
fuqd273 2008-06-24
  • 打赏
  • 举报
回复
嘿嘿,怀疑你的
[相当复杂而且很恶心的处理过程,需要调用许多函数,省略...... ]
程序里面有互斥逻辑。
多线程和单线程一样跑了。

hitytl 2008-06-24
  • 打赏
  • 举报
回复
我用多线程版本程序处理数据时,每个线程的运行时间(线程开始时间采样点从线程创建完毕开始,线程结束时间采样点以线程核心任务
结束为准)会随着数据量的增长而增长。请大家帮我看一下后面的结果,不知道结果是正常还是有问题,谢谢。

线程版本的输出结果:

格式 现在已经结束的线程个数: thread 线程序号 start at[开始时间] and finish at [结束时间] :runtime is [本线程运行时间]

1份数据结果
1: thread 1 start at[1214188090] and finish at [1214188091] :runtime is [1]

10份数据结果
1: thread 2 start at[1214187698] and finish at [1214187702] :runtime is [4]
2: thread 3 start at[1214187698] and finish at [1214187702] :runtime is [4]
3: thread 1 start at[1214187698] and finish at [1214187703] :runtime is [5]
4: thread 7 start at[1214187698] and finish at [1214187703] :runtime is [5]
5: thread 4 start at[1214187698] and finish at [1214187703] :runtime is [5]
6: thread 8 start at[1214187698] and finish at [1214187703] :runtime is [5]
7: thread 6 start at[1214187698] and finish at [1214187703] :runtime is [5]
8: thread 10 start at[1214187698] and finish at [1214187703] :runtime is [5]
9: thread 9 start at[1214187698] and finish at [1214187703] :runtime is [5]
10: thread 5 start at[1214187698] and finish at [1214187703] :runtime is [5]

20份数据结果
1: thread 18 start at[1214187633] and finish at [1214187641] :runtime is [8]
2: thread 7 start at[1214187633] and finish at [1214187642] :runtime is [9]
3: thread 17 start at[1214187633] and finish at [1214187642] :runtime is [9]
4: thread 5 start at[1214187633] and finish at [1214187642] :runtime is [9]
5: thread 20 start at[1214187633] and finish at [1214187643] :runtime is [10]
6: thread 14 start at[1214187633] and finish at [1214187643] :runtime is [10]
7: thread 3 start at[1214187633] and finish at [1214187643] :runtime is [10]
8: thread 4 start at[1214187633] and finish at [1214187643] :runtime is [10]
9: thread 16 start at[1214187633] and finish at [1214187643] :runtime is [10]
10: thread 6 start at[1214187633] and finish at [1214187643] :runtime is [10]
11: thread 1 start at[1214187633] and finish at [1214187644] :runtime is [11]
12: thread 8 start at[1214187633] and finish at [1214187644] :runtime is [11]
13: thread 11 start at[1214187633] and finish at [1214187644] :runtime is [11]
14: thread 9 start at[1214187633] and finish at [1214187644] :runtime is [11]
15: thread 12 start at[1214187633] and finish at [1214187644] :runtime is [11]
16: thread 13 start at[1214187633] and finish at [1214187644] :runtime is [11]
17: thread 15 start at[1214187633] and finish at [1214187644] :runtime is [11]
18: thread 2 start at[1214187633] and finish at [1214187644] :runtime is [11]
19: thread 19 start at[1214187633] and finish at [1214187644] :runtime is [11]
20: thread 10 start at[1214187633] and finish at [1214187644] :runtime is [11]


30份数据结果
1: thread 8 start at[1214187570] and finish at [1214187582] :runtime is [12]
2: thread 1 start at[1214187570] and finish at [1214187583] :runtime is [13]
3: thread 6 start at[1214187570] and finish at [1214187583] :runtime is [13]
4: thread 30 start at[1214187570] and finish at [1214187584] :runtime is [14]
5: thread 27 start at[1214187570] and finish at [1214187584] :runtime is [14]
6: thread 4 start at[1214187570] and finish at [1214187585] :runtime is [15]
7: thread 9 start at[1214187570] and finish at [1214187585] :runtime is [15]
8: thread 17 start at[1214187570] and finish at [1214187585] :runtime is [15]
9: thread 5 start at[1214187570] and finish at [1214187585] :runtime is [15]
10: thread 12 start at[1214187570] and finish at [1214187585] :runtime is [15]
11: thread 11 start at[1214187570] and finish at [1214187585] :runtime is [15]
12: thread 14 start at[1214187570] and finish at [1214187585] :runtime is [15]
13: thread 21 start at[1214187570] and finish at [1214187585] :runtime is [15]
14: thread 19 start at[1214187570] and finish at [1214187585] :runtime is [15]
15: thread 26 start at[1214187570] and finish at [1214187585] :runtime is [15]
16: thread 18 start at[1214187570] and finish at [1214187586] :runtime is [16]
17: thread 22 start at[1214187570] and finish at [1214187586] :runtime is [16]
18: thread 10 start at[1214187570] and finish at [1214187586] :runtime is [16]
19: thread 2 start at[1214187570] and finish at [1214187586] :runtime is [16]
20: thread 20 start at[1214187570] and finish at [1214187586] :runtime is [16]
21: thread 29 start at[1214187570] and finish at [1214187586] :runtime is [16]
22: thread 16 start at[1214187570] and finish at [1214187586] :runtime is [16]
23: thread 28 start at[1214187570] and finish at [1214187586] :runtime is [16]
24: thread 13 start at[1214187570] and finish at [1214187586] :runtime is [16]
25: thread 7 start at[1214187570] and finish at [1214187586] :runtime is [16]
26: thread 25 start at[1214187570] and finish at [1214187586] :runtime is [16]
27: thread 15 start at[1214187570] and finish at [1214187586] :runtime is [16]
28: thread 24 start at[1214187570] and finish at [1214187586] :runtime is [16]
29: thread 23 start at[1214187570] and finish at [1214187586] :runtime is [16]
30: thread 3 start at[1214187570] and finish at [1214187586] :runtime is [16]
hitytl 2008-06-24
  • 打赏
  • 举报
回复
错了,是“不要用gettimeofday,而用time”
hitytl 2008-06-24
  • 打赏
  • 举报
回复
不明白为什么不要用gettimeofday,不用time。能简单说一下“wall clock time”和“进程执行时”么,我对这些概念很不清楚
fierygnu 2008-06-24
  • 打赏
  • 举报
回复
man 2 time
man gettimeofday

要搞清楚wall clock time和进程执行时间的差别。
hitytl 2008-06-24
  • 打赏
  • 举报
回复
我的机器有4个CPU,我把线程体里的所有内容(包括语句和函数调用)完全注释,换成一个什么都不作的循环体,
for(long i=0;i<2500000000;i++)
{
}
当采用1 个线程进行测试,单个进程时间为7秒。(将这个时间计作基本时间)
当采用4 个线程进行测试,4 个进程中运行的时间从7 到12秒都有出现
当采用8 个线程进行测试,8 个进程中运行的时间从7 到19秒都有出现
当采用16个线程进行测试,16个进程中运行的时间从23到34秒都有出现
当采用32个线程进行测试,32个进程中运行的时间从51到63秒都有出现
由于整个线程体中只有for循环体,不含有任何互斥操作,不知道这次测试的时间是否有问题,请帮我检查一下,为
什么每个线程处理的时间还会随着线程数目的增长而增长呢?

另外我对处理时间有下面的理解:
如果单线程处理时间为A,CPU个数为n,假设线程数目为n的k倍,处理时间是应该是(1)是A的k倍呢,(2)还是A的k倍
到2A的k倍呢?不知道对不对。谢谢
fierygnu 2008-06-24
  • 打赏
  • 举报
回复
用time()测量执行时间,不要用gettimeofday。

你的所有线程都是同时启动的,实际的执行时间是10份5,20份11,30份16,正常。
hitytl 2008-06-24
  • 打赏
  • 举报
回复
如果严格按照“尽量避免开大于cpu个数的线程数”,那一次处理4个数据是最理想的了,那其余数据也只好等到下次处理再说了,那这样许许多多的数据还是不能并发处理喽
wooyz 2008-06-24
  • 打赏
  • 举报
回复
如果启动的线程数大于cpu个数,这个时候效率是会有损失的。
所以,请尽量避免开大于cpu个数的线程数。
wooyz 2008-06-23
  • 打赏
  • 举报
回复
用time肯定不准确.
号称intel的vtune,thread profile可以看到线程切换的时序图.
不过工具强大了掌握起来就有点罗嗦.呵呵,我还在摸说中.

如果是windows中,有一个获取cpu/thread时间的api.好象是getsystemtime...
cceczjxy 2008-06-23
  • 打赏
  • 举报
回复
你的进程处理结果是以空间资源换时间得到的,进程远比线程消耗的空间资源多。整个系统内进程是有限资源,线程远比进程多的多。


air_snake 2008-06-23
  • 打赏
  • 举报
回复
你的输出结果呢?估计和你处理的过程有关。随便说说
hitytl 2008-06-23
  • 打赏
  • 举报
回复
程序1:进程版本

main()
{
for(int i=0;i<20;i++)
{
pid=fork()
if(pid==0)
{
process(data);
}
}
}

process(data)
{
struct timeval tv;
gettimeofday(&tv,NULL);
cout < <"START TIME :" < <tv.tv_sec < <endl;

相当复杂而且很恶心的处理过程,需要调用许多函数,省略......

gettimeofday(&tv,NULL);
cout < <"END TIME :" < <tv.tv_sec < <endl;
}

程序2:线程版本

main()
{
for(int i=0;i<20;i++)
{
tid=pthread_create(&tid,NULL,process,data)
if(tid==0)
{
process(data);
}
}
}

void *process(void arg*)
{
struct timeval tv;
gettimeofday(&tv,NULL);
cout < <"START TIME :" < <tv.tv_sec < <endl;

相当复杂而且很恶心的处理过程,需要调用许多函数,省略......

gettimeofday(&tv,NULL);
cout < <"END TIME :" < <tv.tv_sec < <endl;
}

这样应该会的到20组结果,我只是简单的取出START TIME第一次出现的时间和END TIME的最后一次出现时间计算!
hitytl 2008-06-23
  • 打赏
  • 举报
回复
不是用time,而是用函数测试的
struct timeval tv;
gettimeofday(&tv,NULL);
cout<<"START TIME :"<<tv.tv_sec<<endl;
gettimeofday(&tv,NULL);
cout<<"END TIME :"<<tv.tv_sec<<endl;
fuqd273 2008-06-23
  • 打赏
  • 举报
回复
不会用命令来测量的吧

不过不知道楼主是怎么确定测量时间点的。
linaxing 2008-06-23
  • 打赏
  • 举报
回复
你是怎么测量的时间?time命令?

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧