ru Русский

Reticularium

NETWORKS PLACE

I was doing a simple test of pwgen (handy utility to generate passwords) and since this test was creating a lot of files in a folder I decided to combine it with a test of ls speed vs find speed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
homemain ~ # mkdir test
homemain ~ # for ((i=1; i<=300; i++)); do for pw in `pwgen -C`; do touch test/${pw}; done; done
homemain ~ # time (ls test|wc -l)
47685

real  0m0.152s
user  0m0.148s
sys  0m0.008s
homemain ~ # time (find test -type f|wc -l)
47685

real  0m0.072s
user  0m0.024s
sys  0m0.048s

Well, find is faster in mere files listing. On folders with files having more complicated names and differing in size and other attributes the difference is much more significant.

I repeated the time measurement part several times to check if any caching involved, but found no difference.

Ahh, about pwgen: there should be 48000 files. So 48000 – 47685 = 315 filenames appeared to be not unique. Not that bad given that it is a default simple passwords that are easy to remember (e.g. doo5maiJ) without additional symbols etc. The utility has options to make password more secure, I didn’t test it. This pwgen test didn’t have any practical meaning, just curiosity :)

The ‘find vs ls’ test has much more practical meaning though. The result means that in scripts it is much better to use find rather than ls. The ‘find’ utility at the first glance is something more complicated than a “simple” ls. But ls checks and outputs more information about each file and therefore is slower.

Another practical outcome is a simple wrapper equivalent to ‘ls -l’. I found it useful to read e.g. mail folders containing a huge amount of messages:

1
2
#!/bin/sh
find $1 -maxdepth 1 -ls

or copy and paste my command sequence:

1
2
3
4
5
cat > /usr/local/bin/fls << "EOF"
#!/bin/sh
find $1 -maxdepth 1 -ls
EOF
chmod 755 /usr/local/bin/fls

Doing a test:

1
2
3
4
5
6
7
8
9
10
11
12
homemain ~ # time (fls test|wc -l)
47686

real  0m0.218s
user  0m0.140s
sys  0m0.076s
homemain ~ # time (ls -l test|wc -l)
47686

real  0m0.366s
user  0m0.228s
sys  0m0.136s

Comment it: