Re: Profiling, recursive func slower than imperative, normal?
- From: Robert Bossy <Robert.Bossy@xxxxxxxxxxxx>
- Date: Thu, 17 Apr 2008 10:39:08 +0200
Gabriel Genellina wrote:
En Wed, 16 Apr 2008 17:53:16 -0300, <s0suk3@xxxxxxxxx> escribió:Hi,
On Apr 16, 3:27 pm, Jean-Paul Calderone <exar...@xxxxxxxxxx> wrote:
Any function can be implemented without recursion, although it isn'tReally? I'm curious about that, I can't figure out how that would
always easy or fun.
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
In that case, I'm not sure you get any performance gain since the queue has basically the same role as the stack in the recursive version. A definitive answer calls for an actual test, though.
Anyway if you want to process the tree depth-first, the queue version falls in the "not fun" category.
Cheers,
RB
.
- Follow-Ups:
- References:
- Re: Profiling, recursive func slower than imperative, normal?
- From: Jean-Paul Calderone
- Re: Profiling, recursive func slower than imperative, normal?
- From: s0suk3
- Re: Profiling, recursive func slower than imperative, normal?
- Prev by Date: Re: Newbie question about for...in range() structure
- Next by Date: Fwd: is file open in system ? - other than lsof
- Previous by thread: Re: Profiling, recursive func slower than imperative, normal?
- Next by thread: Re: Profiling, recursive func slower than imperative, normal?
- Index(es):
Relevant Pages
|