可以说我有这样的文件内容:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
首先我试过:
time cat temp.txt
输出:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
real 0m0.001s
user 0m0.000s
sys 0m0.001s
其次我试过:
time grep "$" temp.txt
输出:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
real 0m0.002s
user 0m0.000s
sys 0m0.002s
第三我试过:
time awk "/$/" temp.txt
输出:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
real 0m0.004s
user 0m0.001s
sys 0m0.004s
和:
time awk 1 temp.txt
输出:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
real 0m0.004s
user 0m0.000s
sys 0m0.003s
使用 sed:
time sed "" temp.txt
输出:
this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming
real 0m0.002s
user 0m0.000s
sys 0m0.002s
这意味着 cat 是打印所有文件内容的更好命令。因为它执行所需的时间更少。?
答案是“是”。最初,这更像是一个断言,因为 cat 只是在读取文件,而其他两个正在扫描文件以查找表达式。您的
time
脚本是正确的想法,但在这些极短的持续时间内,任何小的差异都会产生错误的结果。最好使用更大的文件,或重复多次。此外(未显示),我多次运行上述命令以确认每个命令大约同时运行,因此是可复制的。
更多基准
根据评论,这里有一些我测试过的命令。在我的系统上,虽然接近了,但效率
grep "^"
并awk "1"
没有明显提高。sed ""
cat
我有相同的脚本。在一个中,我使用的是 cat,而在另一个中,它全是 AWK。
这是第一个:
这是第二个:
第一个脚本所用时间如下(带有 CAT 语句的脚本):
对于只有 AWK 语句的第二个脚本,所用时间如下:
我认为与调用其他外部函数读取文件相比,awk 处理文件要快得多。我很乐意就结果进行讨论。
我认为 AWK 在某些情况下表现更好。