2 ms·
The overhead of the Unix philosophy for such simple steps is gigantic. The example (seq 1 6 | shuf | head -n 1) creates three processes, pipes six strings betwe
by Someone 4y ago
The overhead of the Unix philosophy for such simple steps is gigantic. The example (seq 1 6 | shuf | head -n 1) creates three processes, pipes six strings between the first and the second, and one of them on to the third, and likely calls a RNG six times.
That’s why shuf isn’t a “program that does one thing and does it well”, and allows for a few simple optimization steps:
- use the “-i” option to get rid of the seq process:
shuf -i1-6 | head
- use the “-n” option to get rid of the head process:
shuf -i1-6 -n 1
One process, no pipes, but it probably (I didn’t check the source code) still creates those six strings, and makes those 6 RNG calls.
Even that wastes time and memory. If you’re simulating a large die (say a 10,000-sided one) you may be better of running an awk or Perl script that calls a RNG once.