Mikko Kotila

Topics: Computing, Guides and lists

Computing

How Much Faster Does LC_ALL=C Make Things in Shell?

And how much slower using cat in your commands does? This is also interesting, because cat is one of the first commands people learn in shell, and as a result might end up using it unnecessarily later.

Let’s find out in a very simple test. Below I have included results for doing the exact same thing 4 times in 4 different ways. Basically we are getting a unique row count of a file with ~3 million rows.

Results are:

  1. just using sort without cat and setting LC_ALL=C is fastest and 14.5 faster than slowest which is using cat without LC_ALL=C
  2. using sort without cat and without LC_ALL=C is roughly 3 times slower than doing the same with cat
  3. using cat with or without LC_ALL=C is almost the same

Here the data for reference:

Shell
time LC_ALL=C sort -u fetch.temp | wc -l

real 0m 0.624 s
user 0m 1.027 s
sys 0m 0.252 s

Shell
time sort -u fetch.temp | wc -l

real 0m 3.342 s
user 0m 9.197 s
sys 0m 0.264 s

Shell
time cat fetch.temp | sort -u | wc -l

real 0m 9.352 s
user 0m 8.970 s
sys 0m 0.638 s

Shell
time LC_ALL=C cat fetch.temp | sort -u | wc -l

real 0m 9.378 s
user 0m 8.926 s
sys 0m 0.707 s