Wednesday, December 24, 2014

Fastest Way to Count Files in Dir: ls -f1 | wc -l

$ time set -- *; echo $#         $ time ls | wc -l
real    0m0.715s                    25878                  
user    0m0.678s                  real   0m0.198s 
sys    0m0.010s                   user   0m0.159s
25870                                  sys    0m0.013s

$ time ls -1f | wc -l 
25878                   
real    0m0.045s   
user    0m0.016s  
sys    0m0.007s    

This option is the clear winner, without sorting -f and the -1 for 1 entry per line is what most *nix's will have ls provide to a pipe yet it is better to be explicit.  

No comments:

Post a Comment