从磁盘读取并并行处理 [英] Reading from disk and processing in parallel

查看:178
本文介绍了从磁盘读取并并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是最基本的问题,甚至可能是愚蠢的问题.当我们谈论使用多线程以更好地利用资源时.例如,应用程序从本地文件系统读取和处理文件.可以说,从磁盘读取文件需要5秒钟,而处理则需要2秒钟.

This is going to be the most basic and even may be stupid question here. When we talk about using multi threading for better resource utilization. For example, an application reads and processes files from the local file system. Lets say that reading of file from disk takes 5 seconds and processing it takes 2 seconds.

在上述情况下,我们说使用两个线程(一个线程读取另一个线程)可以节省时间.因为即使一个线程正在处理第一个文件,并行的其他线程也可以开始读取第二个文件.

In above scenario, we say that using two threads one to read and other to process will save time. Because even when one thread is processing first file, other thread in parallel can start reading second file.

问题:这是由于CPU的设计方式引起的.因为其中有不同的处理单元和不同的读/写单元,所以这两个线程甚至可以在同一台核心计算机上并行工作,因为它们实际上是由不同的模块处理的?或这需要多个核心.

Question: Is this because of the way CPUs are designed. As in there is a different processing unit and different read/write unit so these two threads can work in parallel on even a single core machine as they are actually handled by different modules? Or this needs multiple core.

对不起,我很愚蠢. :)

Sorry for being stupid. :)

推荐答案

在单个处理器上,通过时间分片实现多线程.一个线程将完成一些工作,然后将其切换到另一线程.

On a single processor, multithreading is achieved through time slicing. One thread will do some work then it will switch to the other thread.

当线程正在等待某些I/O(例如文件读取)时,它将过早地放弃其CPU时间分片,从而允许另一个线程使用CPU.

When a thread is waiting on some I/O, such as a file read, it will give up it's CPU time-slice prematurely allowing another thread to make use of the CPU.

与单个线程相比,即使在单个内核上,结果也是总体上提高了吞吐量.

The result is overall improved throughput compared to a single thread even on a single core.

下面的键:

  • =在CPU上工作
  • - I/O
  • _空闲
  • = Doing work on CPU
  • - I/O
  • _ Idle

单线程:

====--====--====--====--

两个线程:

====--__====--__====--__
____====--__====--__====

因此,您可以看到在CPU保持繁忙之前要等待的地方,可以同时完成更多工作.存储设备也正在被更多使用.

So you can see how more can get done in the same time as the CPU is kept busy where it would have been kept waiting before. The storage device is also being used more.

这篇关于从磁盘读取并并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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