473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Re: Profiling, recursive func slower than imperative, normal?

On Wed, 16 Apr 2008 13:18:22 -0700 (PDT), sk*******@yahoo.se wrote:
>the 0.409 vs 0.095 is the total times right?
so the imperative function is >4 times faster than the recursive.
or what does tottime stand for?

is this always the case that the recursive function is slower?
the gain is less code?

are some functions only implementable recursively?
Function calls (recursive or otherwise) are more expensive than
for loops, so the version that replaces recursion with a loop is
faster.

Any function can be implemented without recursion, although it isn't
always easy or fun.

Jean-Paul
Jun 27 '08 #1
5 969
On Apr 16, 3:27 pm, Jean-Paul Calderone <exar...@divmod.comwrote:
Any function can be implemented without recursion, although it isn't
always easy or fun.

Jean-Paul
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!).
Jun 27 '08 #2
En Wed, 16 Apr 2008 17:53:16 -0300, <s0****@gmail.comescribió:
On Apr 16, 3:27 pm, Jean-Paul Calderone <exar...@divmod.comwrote:
>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

Jun 27 '08 #3
Gabriel Genellina wrote:
En Wed, 16 Apr 2008 17:53:16 -0300, <s0****@gmail.comescribió:

>On Apr 16, 3:27 pm, Jean-Paul Calderone <exar...@divmod.comwrote:

>>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
Hi,

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
Jun 27 '08 #4
On Apr 17, 9:39 am, Robert Bossy <Robert.Bo...@jouy.inra.frwrote:
Gabriel Genellina wrote:
En Wed, 16 Apr 2008 17:53:16 -0300, <s0s...@gmail.comescribió:
On Apr 16, 3:27 pm, Jean-Paul Calderone <exar...@divmod.comwrote:
>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

Hi,

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.
If you store the folders in a queue (push onto one end and pop from
the other end) the search is breadth-first; if you store the folders
in a stack (push onto one end and pop from the same end) the search is
depth-first. Simple really! :-)
Jun 27 '08 #5

|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.

The potential gain comes from not incurring function call overhead and only
queueing or stacking the exact data needed.

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Irmen de Jong | last post by:
Okay I tried some profiling, but am uncertain about the results I'm getting. They confuse the hell out of me. I have a test program (see below) that essentially has two loops that get called...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
1
by: prathamesh | last post by:
Hi, I am using the Microsoft Visual C++ 6.0 profiler for profiling my c++ application. The output window of the profiler displays the 3 coulms with terms like Func time -- Func+Child time ...
19
by: colin | last post by:
Hi, Im trying to time some parts of my code using the Stopwatch class, but it is so slow, I tried the QueryPerformanceFrequency() but this seems to be just as slow, if I just simply call this...
1
by: skanemupp | last post by:
the 0.409 vs 0.095 is the total times right? so the imperative function is >4 times faster than the recursive. or what does tottime stand for? is this always the case that the recursive function...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.