Re: number of files in directory?
On Tuesday 02 November 2004 07:24 pm, Maurits van Rees wrote:
> Find is actually a lot faster. I timed it on my system, as root in
> the root dir:
>
> mauritsvanrees:/# time ls -1RA | egrep -v "^$" | egrep -c -v ":$"
> 216348
>
> real 4m10.773s
> user 0m14.210s
> sys 0m19.960s
>
> mauritsvanrees:/# time find . -regex ".*" | wc -l
> 216334
>
> real 1m5.149s
> user 0m5.560s
> sys 0m3.900s
> So `find' is roughly four times as fast. The difference in the number
> of files is probably because some files were removed during the
> operation, though I'm not completely sure of that. And `find' also
> counts the current directory, so it finds one more than the other
> solution.
I don't agree with your time results:
robsims@phb:/home/robsims: time ls -1RA | egrep -v "^$" | egrep -c -v ":$"
126062
real 0m26.526s
user 0m0.665s
sys 0m0.658s
robsims@phb:/home/robsims: time find . -regex ".*" | wc -l
126067
real 0m1.651s
user 0m0.617s
sys 0m0.258s
robsims@phb:/home/robsims: time ls -1RA | egrep -v "^$" | egrep -c -v ":$"
126062
real 0m0.664s
user 0m0.469s
sys 0m0.189s
robsims@phb:/home/robsims: time find . -regex ".*" | wc -l
126067
real 0m1.193s
user 0m0.591s
sys 0m0.177s
So yes, the second run was much shorter than the first, but only
because it was second, not because it was inherently faster. Beware
the effects of caching when benchmarking. You can shorten the ls
command line:
time ls -1RA | egrep -c -v -e "^$" -e ":$" with no significant change
in execution time, but fixing the find line:
robsims@phb:/home/robsims: time find . | wc -l
126067
real 0m0.295s
user 0m0.119s
sys 0m0.177s
...helps quite a bit. My count differents correlates to one "."
entry and four directories ending in a colon.
--
Rob
Reply to: