473,320 Members | 1,863 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,320 software developers and data experts.

Profiling, recursive func slower than imperative, normal?

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?

def power(nbr, po):
if po==0:
return 1
if po>0:
return nbr*power(nbr, po-1)
if po<0:
return 1/power(nbr, -1*po)

109992 function calls (10002 primitive calls) in 0.409 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall
filename:lineno(function)
1 0.015 0.015 0.409 0.409 <pyshell#217>:1(test1)
1 0.000 0.000 0.409 0.409 <string>:1(<module>)
109989/9999 0.394 0.000 0.394 0.000 myMath.py:39(power)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}

def power2(nbr, po):
acc=1
if po >= 1:
acc=nbr
for x in range(1, po):
acc=acc*nbr
if po < 0:
if nbr!=0:
acc=1
for x in range(0, po, -1):
acc=acc/nbr
else:
return "Division by zero"
return acc
20001 function calls in 0.095 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall
filename:lineno(function)
1 0.026 0.026 0.095 0.095 <pyshell#221>:1(test1)
1 0.000 0.000 0.095 0.095 <string>:1(<module>)
9999 0.051 0.000 0.069 0.000 myMath.py:47(power2)
1 0.000 0.000 0.000 0.000 {method 'disable' of
'_lsprof.Profiler' objects}
9999 0.017 0.000 0.017 0.000 {range}
Jun 27 '08 #1
1 1045

<sk*******@yahoo.sewrote in message
news:fd**********************************@k1g2000p rb.googlegroups.com...
| 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?

Computers can be viewed as linear iteration machines implementing

while True:
execute next instruction and set next pointer appropriately

But algorithms most naturally expressed with multiple recursion usually
look more ungainly when linearized.


Jun 27 '08 #2

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...
5
by: Jean-Paul Calderone | last post by:
On Wed, 16 Apr 2008 13:18:22 -0700 (PDT), skanemupp@yahoo.se wrote: Function calls (recursive or otherwise) are more expensive than for loops, so the version that replaces recursion with a loop...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.