数词:什么会使用更少的 CPU、wc 或 perl 脚本?

yae*_*ael 0 linux perl wc

我的目标是在使用尽可能少的 CPU 的同时计算文件中的单词。

我可以使用该wc命令或为此编写一个简单的 perl 脚本,这两个选项中哪个 CPU 密集度较低?

win*_*ner 5

我怀疑它wc会使用更少的 CPU 时间,它是用 C 编写的,并且随着时间的推移经过了仔细的优化。但是,没有理由不亲自测试。time将让您将 perl 脚本的性能与wc. 使用该time命令,您应该得到如下输出:

> time wc /usr/share/dict/words
 119095  119095 1145922 /usr/share/dict/words
wc /usr/share/dict/words  0.03s user 0.00s system 98% cpu 0.030 total
Run Code Online (Sandbox Code Playgroud)

在这种情况下,wc总共使用了 0.030 秒的 CPU 时间。