1

(Inspired by the comments for this question)

Why is ls this much slower than echo *?

$ time bash -c 'for i in {1..10000}; do ls -f > /dev/null; done'
bash -c 'for i in {1..10000}; do ls -f > /dev/null; done' 7.49s user 5.39s system 108% cpu 11.883 total
$ time bash -c 'for i in {1..10000}; do echo * > /dev/null; done'
bash -c 'for i in {1..10000}; do echo * > /dev/null; done' 0.16s user 0.25s system 98% cpu 0.415 total

I mean, sure, ls being slightly slower than echo * makes sense, but this seems ridiculous.

Why would ls be nearly be the case?

If it has to do with ls not needing the speed, then there's no reason for yes to be so fast.

What is ls doing under the hood that makes it so slow?

(I'm using zsh on Arch Linux, on a laptop. Times are similar on bash. I've also tested on a (Linux Mint) desktop computer with similar results. (There, ls -f is faster at around 7 seconds, echo * is still about 0.4 seconds, which is still a ridiculous difference.) On Arch Linux, ls was also unaliased, but I didn't bother for Mint, and it didn't make a difference anyway.)

Andy Dalton
  • 13,993
FaeFeyFa
  • 13
  • 6

1 Answers1

0

Using strace, we can see echo makes far less syscalls to the kernel compared to ls:

root@4a21b0630cba:/# strace -c ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         7           read
  0.00    0.000000           0         1           write
  0.00    0.000000           0        10           close
  0.00    0.000000           0         9           fstat
  0.00    0.000000           0        26           mmap
  0.00    0.000000           0         9           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         2           rt_sigaction
  0.00    0.000000           0         1           rt_sigprocmask
  0.00    0.000000           0         2           ioctl
  0.00    0.000000           0         8           pread64
  0.00    0.000000           0         2         2 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2         2 statfs
  0.00    0.000000           0         2         1 arch_prctl
  0.00    0.000000           0         2           getdents64
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         8           openat
  0.00    0.000000           0         1           set_robust_list
  0.00    0.000000           0         1           prlimit64
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    99         5 total
root@4a21b0630cba:/# 
root@4a21b0630cba:/# 
root@4a21b0630cba:/# 
root@4a21b0630cba:/# strace -c echo *
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         1           read
  0.00    0.000000           0         1           write
  0.00    0.000000           0         4           close
  0.00    0.000000           0         3           fstat
  0.00    0.000000           0         7           mmap
  0.00    0.000000           0         4           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         6           pread64
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         2         1 arch_prctl
  0.00    0.000000           0         2           openat
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    36         2 total
root@4a21b0630cba:/# 

GMaster
  • 6,322