你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作。数据专家们,我是在对你们说。你可能有一个4核或更多核的CPU,但我们合适的工具,例如 grepbzip2wcawksed等等,都是单线程的,只能使用一个CPU内核。

借用卡通人物Cartman的话,“如何我能使用这些内核”?

要想让Linux命令使用所有的CPU内核,我们需要用到GNU Parallel命令,它让我们所有的CPU内核在单机内做神奇的map-reduce操作,当然,这还要借助很少用到的–pipes 参数(也叫做–spreadstdin)。这样,你的负载就会平均分配到各CPU上,真的。

BZIP2

bzip2是比gzip更好的压缩工具,但它很慢!别折腾了,我们有办法解决这问题。

以前的做法:

cat bigfile.bin | bzip2 --best > compressedfile.bz2

现在这样:

cat bigfile.bin | parallel --pipe --recend '' -k bzip2 --best > compressedfile.bz2

尤其是针对bzip2,GNU parallel在多核CPU上是超级的快。你一不留神,它就执行完成了。

GREP

如果你有一个非常大的文本文件,以前你可能会这样:

grep pattern bigfile.txt

现在你可以这样:

cat bigfile.txt | parallel  --pipe grep 'pattern'

或者这样:

cat bigfile.txt | parallel --block 10M --pipe grep 'pattern'

这第二种用法使用了 –block 10M参数,这是说每个内核处理1千万行——你可以用这个参数来调整每个CUP内核处理多少行数据。

AWK

下面是一个用awk命令计算一个非常大的数据文件的例子。

常规用法:

cat rands20M.txt | awk '{s+=$1} END {print s}'

现在这样:

cat rands20M.txt | parallel --pipe awk \'{s+=\$1} END {print s}\' | awk '{s+=$1} END {print s}'

这个有点复杂:parallel命令中的–pipe参数将cat输出分成多个块分派给awk调用,形成了很多子计算操作。这些子计算经过第二个管道进入了同一个awk命令,从而输出最终结果。第一个awk有三个反斜杠,这是GNU parallel调用awk的需要。

WC

想要最快的速度计算一个文件的行数吗?

传统做法:

wc -l bigfile.txt

现在你应该这样:

cat bigfile.txt | parallel  --pipe wc -l | awk '{s+=$1} END {print s}'

非常的巧妙,先使用parallel命令‘mapping’出大量的wc -l调用,形成子计算,最后通过管道发送给awk进行汇总。

SED

想在一个巨大的文件里使用sed命令做大量的替换操作吗?

常规做法:

sed s^old^new^g bigfile.txt

现在你可以:

cat bigfile.txt | parallel --pipe sed s^old^new^g

…然后你可以使用管道把输出存储到指定的文件里。

http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/

Utilizing multi core for tar+gzip/bzip compression/decompression

You can use pigz(http://zlib.net/pigz/) instead of gzip, which does gzip compression on multiple cores. Instead of using the -z option, you would pipe it through pigz:

tar cf - paths-to-archive | pigz > archive.tar.gz
By default, pigz uses the number of available cores, or eight if it could not query that. You can ask for more with -p n, e.g. -p 32. pigz has the same options as gzip, so you can request better compression with -9. E.g.

tar cf - paths-to-archive | pigz -9 -p 32 > archive.tar.gz

You can also use the tar flag "--use-compress-program=" to tell tar what compression program to use.
For example use:
tar -c --use-compress-program=pigz -f tar.file dir_to_zip

Common approach

There is option for tar program:

-I, --use-compress-program PROG
filter through PROG (must accept -d)
You can use multithread version of archiver or compressor utility.

Most popular multithread archivers are pigz (instead of gzip) and pbzip2 (instead of bzip2). For instance:

$ tar -I pbzip2 -cf OUTPUT_FILE.tar.bz2 paths_to_archive
$ tar --use-compress-program=pigz -cf OUTPUT_FILE.tar.gz paths_to_archive
Archiver must accept -d. If your replacement utility hasn't this parameter and/or you need specify additional parameters, then use pipes (add parameters if necessary):

$ tar cf - paths_to_archive | pbzip2 > OUTPUT_FILE.tar.gz
$ tar cf - paths_to_archive | pigz > OUTPUT_FILE.tar.gz
Input and output of singlethread and multithread are compatible. You can compress using multithread version and decompress using singlethread version and vice versa.

http://stackoverflow.com/questions/12313242/utilizing-multi-core-for-targzip-bzip-compression-decompression

如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等(转)的更多相关文章

  1. 【转】如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等   你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并 ...

  2. <转>如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    原文链接:http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB) ...

  3. 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...

  4. 如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://blog.chinaunix.net/uid-20662820-id-4023733.html http://www.faqs.org/faqs/snmp-faq/part2/ http ...

  5. 如何利用多核CPU来加速你的Linux命令

    原文出处: rankfocus   译文出处: 外刊IT评论 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作.数据专家们,我是在对你们说.你可能 ...

  6. python多线程不能利用多核cpu,但有时候多线程确实比单线程快。

    python 为什么不能利用多核 CPU  GIL 其实是因为在 python中有一个 GIL( Global Interpreter Lock),中文为:全局解释器锁.  1.最开始时候设计GIL是 ...

  7. python学习笔记(二十九)为什么python的多线程不能利用多核CPU

    问题:为什么python的多线程不能利用多核CPU,但是咱们在写代码的时候,多线程的确是在并发,而且还比单线程快原因:因为GIL,python只有一个GIL,运行python时,就要拿到这个锁才能执行 ...

  8. python多线程为什么不能利用多核cpu

    GIL 与 Python 线程的纠葛 GIL 是什么东西?它对我们的 python 程序会产生什么样的影响?我们先来看一个问题.运行下面这段 python 程序,CPU 占用率是多少? # 请勿在工作 ...

  9. 利用多核来加速Linux命令行

    本文转载自 多核CPU来加速 awk, sed, bzip2, grep, wc等,如需查看原文,请点此链接进入. -------------------------------我是分割线 开始 -- ...

随机推荐

  1. DMP文件的生成和使用

    1.生成dmp的程序 #include  <dbghelp.h> #pragma comment(lib,  "dbghelp.lib") //设置异常处理回调函数Se ...

  2. Python的对象和类型

    Python使用对象来存储数据,构造任何类型的值都是一个对象. 任何一个对象都有三个特性:身份,类型和值. 身份是对象的唯一标识,可以通过内建函数id()得到,这个值可以认为是该对象的内存地址. Py ...

  3. 运用mapreduce计算tf-idf

    问题描写叙述:给定一个大文件,文件里的内容每一行为:文档名,文档内容. input 文档名1,word1 Word2 ....... 文档名2,word1 Word2 ....... output w ...

  4. poj3621 Sightseeing Cows --- 01分数规划

    典型的求最优比例环问题 參考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每一个点和每条边的权值,求一个环使 ans=∑点 ...

  5. NET之全平台一体化

    NET之全平台一体化的体验 一.前言 近来利用空闲时间研究了一下Xamarin的技术,想想既然提供了如此好的支持,就该尝试一切可能,来一个”大小通吃“. 何为全平台:APP包括Android.IOS. ...

  6. Activity数据传输到服务

    activity数据接口负责启动该服务包.service获取数据.手术. 详细demo如下面: package com.example.android_service_trance; import a ...

  7. Centos 6安装完美搭建mysql、php、apache之旅

    安装apache [root@centos share]# yum -y install httpd Loaded plugins: fastestmirror, refresh-packagekit ...

  8. 【CSS3】transform-origin原点旋转

    忙乱, 点 -moz-transform-origin: 0 0; -webkit-transform-origin:0 0; -o-transform-origin:0 0; 以右上角给原点 -mo ...

  9. hdu1028(整数划分问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题 整数划分 --- 一个老生长谈的问题: 描述 整数划分是一个经典的问题.请写一个程 ...

  10. 依据二度人脉推荐好友sql

    friend表结构 DROP TABLE IF EXISTS FRIEND; create table friend(     uid        bigint not null comment ' ...