多线程会提高性能吗?如何? [英] Does multi-threading improve performance? How?

查看:347
本文介绍了多线程会提高性能吗?如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到每个人都在谈论多线程如何提高性能.我不相信这一点,除非我缺少一些东西.如果我有一个包含100个元素的数组,则遍历它需要6秒钟.当我在两个线程之间分配工作时,处理器将不得不完成相同数量的工作,因此也要花费时间,除了它们同时工作但速度减半.多线程难道不应该让它变得更慢吗?因为您需要其他说明来划分工作?

I hear everyone talking about how multi-threading can improve performance. I don't believe this, unless there is something I'm missing. If I have an array of 100 elements and traversing it takes 6 seconds. When I divide the work between two threads, the processor would have to go through the same amount of work and therefore time, except that they are working simultaneously but at half the speed. Shouldn't multi threading make it even slower? Since you need additional instructions for dividing the work?

推荐答案

对于迭代100个元素的简单任务,多线程处理不会带来性能上的好处.

For a simple task of iterating 100 elements multi-threading the task will not provide a performance benefit.

迭代超过1000亿个元素并在每个元素上进行处理,然后使用额外的CPU可能会帮助减少处理时间.例如,更复杂的任务可能会由于I/O而导致中断.当一个线程正在睡眠中等待外围设备完成I/O(例如磁盘写操作或键盘上的按键操作)时,其他线程可以继续其工作.

Iterating over 100 billion elements and do processing on each element, then the use of additional CPU's may well help reduce processing time. And more complicated tasks will likely incur interrupts due to I/O for example. When one thread is sleeping waiting for a peripheral to complete I/O (e.g. a disk write, or a key press from the keyboard), other threads can continue their work.

这篇关于多线程会提高性能吗?如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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