PTT
Submit
Submit
选择语言
正體中文
简体中文
PTT
C_and_CPP
[问题] 使用thread更慢的问题
楼主:
a2975313
(GN)
2016-03-23 00:50:40
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
windows 10, visual studio 2013
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
google v8
问题(Question):
一样的逻辑搬到thread去, 计时后发现反而更慢, 难道open thread的cost比我的逻辑
还重吗? 有没有人知道其中的原理.
程式码(Code):(请善用置底文网页, 记得排版)
主要功能是 (readfile→compile→run)*4, 然后计时此round花了多久时间.
但NonThread跑1000个round平均≒0.000812/s
使用Thread跑1000个round平均≒0.004850/s
我并非计时thread里面的时间, 而是计时open thread的时间, 这样还差了6倍 很怪~
http://imgur.com/l7jNKWJ
http://imgur.com/9jnAS1o
楼主:
a2975313
(GN)
2016-03-23 13:58:00
critical section和mutex是实作lock的方式如果使用join那就必须等到thread里面执行完才能接下去跑只是没想到open thread比thread里面做的事情还花时间
作者: LiloHuang (十年一刻)
2016-03-23 12:10:00
VC++ 的 std::async() 底层已经有用 thread pool如果没记错,也仅只于 VC++ 的实作有如此完善 :)如果是我会直接用 Intel TBB library 发 tbb::task或用微软的 PPL 发 task 来做 task-based parallelism稍微看了一下你的程式码,还有 lock contention 的问题除了 Caesar08 提到的得 join 之外,你得想办法移掉那个 critical secton,例如使用 thread local storage如果上面都做了还是慢,装一下 Intel VTune 分析一下fix typo : section
作者:
Caesar08
(Caesar)
2016-03-23 09:13:00
1.计时的部分,有个东西叫做chrono2.你的key能保证在EnterCriticalSection时,每个都不同吗?如果不行,你没办法最大化效能3.你的thread没做join,程式居然没崩溃4.在你这情况,你用async会比你用thread好5.construct一个thread的成本的确很高,如果你要最大化效能,考虑使用threadpool6.我的拙作
https://github.com/Fdhvdu/ThreadPool
作者: TeaEEE (爱不趴 不爱趴)
2016-03-23 10:04:00
我猜是readfile这卡住你的thread效益
继续阅读
[问题] atomic_compare_exchange与shared_ptr
Caesar08
[问题] 利用curl 判断是档案还是网页
justfor0223
[问题] Undefined reference, symbol error问题
henry8168
[问题] RS232 ReadFile的buffer
sava31
[问题] 传入函式的buffer长度判断问题
petercoin
Re: [问题] 输入太多字元会造成程式hang掉。
LPH66
Re: [问题] Deep learning net参数调整
yoco
[问题] 关于SDK编译的问题
jimmy4205
Re: [问题] 输入太多字元会造成程式hang掉。
ResolaQQ
[问题] 输入太多字元会造成程式hang掉。
jay70120
Links
booklink
Contact Us: admin [ a t ] ucptt.com