c + +多线程升压比,因为CPU类型的单线程慢? [英] C++ Boost Multithread is slower than single thread because of CPU type?

查看:111
本文介绍了c + +多线程升压比,因为CPU类型的单线程慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前贴一些提升多线程。这一次,我只是好奇和失望,因为我认为多线程假设是比单的速度更快。

两个线程文件I / O读/解析CSV数据。当我用多线程,花了约40秒每台机器的奔腾D CPU平均从戴尔台式OPTILEX 745

使用一个线程,花了大约8-10秒平均同一台PC。

我曾试图从这些两个线程使用完全不同的参数的名字,结果是没有什么不同。如果有人已经使用C ++提高多线程读取大数据文件和分析之前,我很想听听你的意见。谢谢。
安德鲁


解决方案

  

两个线程文件I / O读/解析CSV数据。


如果他们正在阅读相同的文件句柄同一个文件,那么他们可能会花费大量的时间阻塞,在等待另一个线程得到完成。如果他们使用不同的文件句柄来读取相同的文件,他们正在迫使盘保持寻求来回,这是不一样的直线顺序读取高效的操作。

线程不会加快大文件读取和解析。它的作用是让你做别的东西完全在读取文件和解析。

您已经创建了一个I / O瓶颈,这线程不与帮助。线程是用于降低CPU的瓶颈时,该算法可以被分成独立的执行线程;有很多在previous输出相关性(文件的解析是一个案例)的算法一般不需要线程好。

如果您可以拆分解析问题,让每个线程解析文件的不同部分,你可能会得到一点点改善,但可能不会因为追求会浪费你的时间。如果你可以有一个线程执行批量读取和一些preprocessing,然后换手块一个线程池为真正的重型加工(是疗法的?),那么你可能会看到比单线程一些显着的改进。

这是所有普通和位流的意识,但很难做你给什么等等。我希望它能帮助。

I had posted some boost multithreads before. This time I just curious and disappointed because I thought multithreads suppose to be faster than single one.

Two threads are FILE I/O read/parse the CSV data. When I used multithreads, it took about 40 seconds average per machine PENTIUM D CPU from DELL DESKTOP OPTILEX 745.

With single thread, it took about 8-10 seconds average same PC.

I had tried to use completely different parameters name from these two threads, the result is no different. If someone had been used c++ boost multi-threads for reading big data file and parsing before, I would love to hear your opinions. Thanks. Andrew

解决方案

Two threads are FILE I/O read/parse the CSV data.

If they're reading the same file with the same file handle, then they might be spending most of their time blocked waiting for the other thread to get done. If they're using different file handles to read the same file, they're forcing the disk to keep seeking back and forth, which isn't as efficient an operation as a straight sequential read.

Threading doesn't speed up big file reading and parsing. What it does is let you do something else entirely while the file is being read and parsed.

You've created an I/O bottleneck, which threading does not help with. Threading is intended for reducing CPU bottlenecks when the algorithm can be broken into independent threads of execution; algorithms that have a lot of dependency on previous output (file parsing is one case) generally don't thread well.

If you can split up the parsing problem and have each thread parse a different part of the file, you might get a little improvement, but probably not since the seeking will be wasting your time. If you can have one thread doing bulk reading and some preprocessing, then handing off chunks to a thread pool for the real heavy processing (is ther any?), then you might see some noticeable improvement over single threading.

This is all general and a bit stream-of-consciousness, but it's hard to do much more with what you're giving. I hope it helps.

这篇关于c + +多线程升压比,因为CPU类型的单线程慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆