enn*_*ler 3 perl performance multithreading
所以我上了Core 2 Duo MacBook上运行的Perl 5.10亲们编译线程支持:usethreads=define,useithreads=define.我有一个简单的脚本来读取4个gzip文件,每个文件包含750000行.我正在使用Compress :: Zlib来解压缩和读取文件.我有两个实现,它们之间的唯一区别是一个包含use threads.除此之外,两个脚本都运行相同的子程序来进行读取.因此,在伪代码中,非线程程序执行此操作:
read_gzipped(file1);
read_gzipped(file2);
read_gzipped(file3);
read_gzipped(file4);
Run Code Online (Sandbox Code Playgroud)
线程版本如下:
my thr0 = threads->new(\$read_gzipped,'file1')
my thr1 = threads->new(\$read_gzipped,'file1')
my thr2 = threads->new(\$read_gzipped,'file1')
my thr3 = threads->new(\$read_gzipped,'file1')
thr0->join()
thr1->join()
thr2->join()
thr3->join()
Run Code Online (Sandbox Code Playgroud)
现在,线程版本的运行速度几乎比非线程脚本快2倍.这显然不是我希望的结果.谁能解释我在这里做错了什么?
我的猜测是GZIP操作的瓶颈是磁盘访问.如果您有四个线程在platter硬盘上竞争磁盘访问,那么这会大大减慢速度.磁盘头必须快速连续移动到不同的文件.如果您一次只处理一个文件,则磁头可以保持在该文件附近,并且磁盘缓存将更准确.
| 归档时间: |
|
| 查看次数: |
329 次 |
| 最近记录: |