Profiling, recursive func slower than imperative, normal?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Apr 16 18:29:14 EDT 2008
En Wed, 16 Apr 2008 17:53:16 -0300, <s0suk3 at gmail.com> escribió:
> On Apr 16, 3:27 pm, Jean-Paul Calderone <exar... at divmod.com> wrote:
>
>> Any function can be implemented without recursion, although it isn't
>> always easy or fun.
>>
> Really? I'm curious about that, I can't figure out how that would
> work. Could give an example? Say, for example, the typical: walking
> through the file system hierarchy (without using os.walk(), which uses
> recursion anyway!).
Use a queue of pending directories to visit:
start with empty queue
queue.put(starting dir)
while queue is not empty:
dir = queue.get()
list names in dir
for each name:
if is subdirectory: queue.put(name)
else: process file
--
Gabriel Genellina
More information about the Python-list
mailing list