给定一个包含 m 行的文件,如何获取第 n 行。 m 有时可能小于 n。我努力了:
method1: sed -ne '10p' file.txt
method2: sed -ne '10p' <file.txt
method3: sed -ne '10{p;q;}' file.txt
method4: awk 'NR==10' file.txt
在 LeetCode 的https://leetcode.com/problems/tenth-line/。方法1击败其他方法。我不知道为什么。我认为方法3应该更快。
有更快的方法吗?
更新:
按照@skwllsp的建议,我运行了一些命令。结果是: 说明
instructions commands
428,160,537 perf stat sed -ne '10p' file.txt
427,426,310 perf stat sed -ne '10p' <file.txt
1,033,730 perf stat sed -ne '10{p;q;}' file.txt
1,111,502 perf stat awk 'NR == 10 { print ; exit ;} ' file.txt
method4已根据@Archemar的回答进行了更改
和
777,525 perf -stat tail -n +10 file.txt |head -n 1
这比方法1要少得多。
答案1
让我们测量您的测试,看看每种方法执行了多少条指令。我创建了自己的文件 seq 2000000 > 2000000.txt
,我想找到哪种方法最快。
$ perf stat sed -ne '10p' 2000000.txt
10
Performance counter stats for 'sed -ne 10p 2000000.txt':
203.877247 task-clock # 0.991 CPUs utilized
5 context-switches # 0.025 K/sec
3 cpu-migrations # 0.015 K/sec
214 page-faults # 0.001 M/sec
405,075,423 cycles # 1.987 GHz [50.20%]
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
838,221,677 instructions # 2.07 insns per cycle [75.20%]
203,113,013 branches # 996.251 M/sec [74.99%]
766,918 branch-misses # 0.38% of all branches [75.16%]
0.205683270 seconds time elapsed
所以第一种方法 - 838,221,677 指令。
$ perf stat sed -ne '10{p;q;}' 2000000.txt
10
Performance counter stats for 'sed -ne 10{p;q;} 2000000.txt':
1.211558 task-clock # 0.145 CPUs utilized
2 context-switches # 0.002 M/sec
0 cpu-migrations # 0.000 K/sec
213 page-faults # 0.176 M/sec
1,633,950 cycles # 1.349 GHz [23.73%]
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
824,789 instructions # 0.50 insns per cycle
164,935 branches # 136.135 M/sec
11,751 branch-misses # 7.12% of all branches [83.24%]
0.008374725 seconds time elapsed
所以,第三种方法 - 824,789 条指令。比第一种方法好很多。
改进的第四种方法
$ perf stat awk 'NR == 10 { print ; exit ;} ' 2000000.txt
10
Performance counter stats for 'awk NR == 10 { print ; exit ;} 2000000.txt':
1.357354 task-clock # 0.162 CPUs utilized
2 context-switches # 0.001 M/sec
0 cpu-migrations # 0.000 K/sec
282 page-faults # 0.208 M/sec
1,777,749 cycles # 1.310 GHz [11.54%]
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
919,636 instructions # 0.52 insns per cycle
185,695 branches # 136.807 M/sec
11,218 branch-misses # 6.04% of all branches [91.64%]
0.008375258 seconds time elapsed
比第二种方法差一点。无论如何,它和第三种方法一样有效。
您可以对您的文件重复相同的测试,看看哪种方法最好。
第二种方法的测量:
$ perf stat sed -ne '10p' <2000000.txt
10
Performance counter stats for 'sed -ne 10p':
203.278584 task-clock # 0.998 CPUs utilized
1 context-switches # 0.005 K/sec
3 cpu-migrations # 0.015 K/sec
213 page-faults # 0.001 M/sec
403,941,976 cycles # 1.987 GHz [49.84%]
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
835,372,994 instructions # 2.07 insns per cycle [74.92%]
203,327,145 branches # 1000.239 M/sec [74.90%]
773,067 branch-misses # 0.38% of all branches [75.35%]
0.203714402 seconds time elapsed
和第一种方法一样糟糕