My objective is to count the words in a file while using the minimum possible CPU.

I could use the wc command or write a simple perl script for this, which of the two options will be less CPU intensive?

| improve this question | | | | |

I suspect that wc will use less CPU time, it is written in C and has been carefully optimized over time. However, there is no reason not to test this yourself. time will let you compare the performance of your perl script against wc. Using the time command, you should get output like:

> 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

In this case, wc used 0.030 seconds of CPU time in total.

| improve this answer | | | | |

It's generally the case that when trying to solve a problem that can be completely encapsulated by a single command line tool such as wc, that they're more performant than anything else.

It's only when you have to start chaining multiple "commands" together does it make sense to transition the solution from chaining them together, to solving the problem in a higher level language such as Perl, Python, or Ruby.

The expense in chaining the commands together usually comes in the form of having to repetitively keep executing the simple tools many times over.

| improve this answer | | | | |

I agree with @wingedsubmariner, wc will almost certainly be faster than any perl version you cook up:

$ /usr/bin/time -f "%Uuser %Ssystem %Eelapsed %PCPU" wc /usr/share/dict/words
 99171  99171 938848 /usr/share/dict/words
0.02user 0.00system 0:00.02elapsed 86%CPU
$ /usr/bin/time -f "%Uuser %Ssystem %Eelapsed %PCPU" \
  perl -ane '$k+=$#F+1; END{print "$k\n"}' /usr/share/dict/words
0.09user 0.00system 0:00.09elapsed 96%CPU
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.