[REBOL] Re: Reading directories faster.
From: brett:codeconscious at: 9-Oct-2002 9:31
> either none? df: attempt [read %./][
> "Error reading Directory"
> foreach file df [insert tail either (dir? file)[dirs][files] file]
> Any ideas on how to speed it up?
The dir? function is based on the info? function. info? queries the target
system to get the attributes of the target.
But the results of read %./ already contains enough information about
whether an item is a file or a directory - just look for a slash #"/" at the
end of the name. So by using dir? you end up making an unnecessary call to
the file system for every file.
So try changing the code to:
either none? df: attempt [read %./][
"Error reading Directory"
foreach file df [insert tail either #"/" = last file [dirs][files]
On my system your code took 9 - 10 seconds. After the change, approximately
0.05 of a second.
Also, don't forgot to preallocate the dirs and the files block! or list! or
whatever you are using to be a "reasonable size".
files: make block! 2000
This avoids a lot of the time taken to automatically expand the block as it
is being filled.