如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等(转)
你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作。数据专家们,我是在对你们说。你可能有一个4核或更多核的CPU,但我们合适的工具,例如 grep, bzip2, wc, awk, sed等等,都是单线程的,只能使用一个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等(转)的更多相关文章
- 【转】如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并 ...
- <转>如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
原文链接:http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB) ...
- 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...
- 如何利用多核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 ...
- 如何利用多核CPU来加速你的Linux命令
原文出处: rankfocus 译文出处: 外刊IT评论 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作.数据专家们,我是在对你们说.你可能 ...
- python多线程不能利用多核cpu,但有时候多线程确实比单线程快。
python 为什么不能利用多核 CPU GIL 其实是因为在 python中有一个 GIL( Global Interpreter Lock),中文为:全局解释器锁. 1.最开始时候设计GIL是 ...
- python学习笔记(二十九)为什么python的多线程不能利用多核CPU
问题:为什么python的多线程不能利用多核CPU,但是咱们在写代码的时候,多线程的确是在并发,而且还比单线程快原因:因为GIL,python只有一个GIL,运行python时,就要拿到这个锁才能执行 ...
- python多线程为什么不能利用多核cpu
GIL 与 Python 线程的纠葛 GIL 是什么东西?它对我们的 python 程序会产生什么样的影响?我们先来看一个问题.运行下面这段 python 程序,CPU 占用率是多少? # 请勿在工作 ...
- 利用多核来加速Linux命令行
本文转载自 多核CPU来加速 awk, sed, bzip2, grep, wc等,如需查看原文,请点此链接进入. -------------------------------我是分割线 开始 -- ...
随机推荐
- __NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking 我的平台 mac os 10.6 Xcode ...
- ISO/OSI网络体系结构和TCP/IP协议模型
1. ISO/OSI的参考模型共有7层,由低层至高层分别为:物理层.数据链路层.网络层.传输层.会话层.表示层. 应用层.各层功能分别为: (1)物理层 提供建立.维护和拆除 ...
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- POJ2584 T-Shirt Gumbo【二分图多重匹配】
题目链接: id=2584">http://poj.org/problem?id=2584 题目大意: 如今有5种型号(S.M.L.X.T)的衣服要发放给N个參赛队员.给出每一个參赛者 ...
- [Network]Wireless and Mobile
Wireless 1 Introduction 1.1 Elements 1. Wireless Hosts Wireless does not mean mobility. 2. Base Stat ...
- VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)
原文:VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel) private void AddRightMenu() { Microsoft ...
- Mod in math
An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...
- Enum实现单例模式
package com.wjy.effective; public enum Singleton { INSTANCE; private int numa; private int numb; pub ...
- SE 2014年4月14日
一. 概述BGP的特点 BGP协议是一种距离矢量协议,基于TCP的179端口,BGP协议不会动态的学习路由,只能将IGP协议学习到的或者静态路由注入到BGP中,成为BGP路由,BGP路由携带有丰富的路 ...
- Java如何检查List<String> 里是否有想要的字符串?
List<String> test = new ArrayList<String>(); test.add("a"); test.add("b&q ...