473,397 Members | 2,028 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,397 software developers and data experts.

bad generator performance

hi,

i am in the process of profiling an application and noticed how much
time my depth first generator for walking a tree data structure took.

i wrote the same thing as a recursive function producing an array, and
this non-generator version is nearly 10 times faster!

any ideas why this is, what i did wrong...
for some reason the generator version appears as being called much more
often than the recursive one. no idea why that is, but the data is
definitely identical!

here the output from the python profiler and the respective code:

### snip ###

ncalls tottime percall cumtime percall filename:lineno(function)
94209/8191 0.560 0.000 0.590 0.000 :71(depthFirstIterator2)
4095/1 0.060 0.000 0.080 0.080 :62(depthFirstIterator1)

### snip ###

def depthFirstIterator1(self, depth = 0):
ret = [[self, True, depth]]

if self.isFolder():
for c in self.children:
ret = ret + c.depthFirstIterator(depth = depth + 1)

return ret + [[self, False, depth]]

def depthFirstIterator2(self, depth = 0):
yield [self, True, depth]

if self.isFolder():
for c in self.children:
for y in c.depthFirstIterator(depth = depth + 1):
yield y

yield [self, False, depth]

### snip ###

i'd appreciate any comments or explanations why the generator might be
so much slower, as i had just decided to use generators more frequently
and in the respective PEP they are praised as generally fast.

thx,

Johannes
Jul 18 '05 #1
2 1271
sorry, forgot to post the profiling info for the recursive helper
function.
but generator is still FAR slower...

4095/1 0.050 0.000 0.120 0.120 file.py:135(rek)

Johannes
Jul 18 '05 #2
Johannes Ahl-mann <so*****@gmx.net> wrote:
a non-recursive solution to traversing a recursive data type is bound to
get ugly, isn't it?


Not necessarily: sometimes using an explicit stack can be quite pretty,
depending. E.g.:

def all_leaves(root):
stack = [root]
while stack:
rightmost = stack.pop()
if is_leaf(rightmost):
yield rightmost
else:
stack.extend(rightmost.all_children())

This isn't the traversing you're looking for, but it is _a_ traversing
of a recursive data type, non-recursive, and IMHO quite pretty.
Alex
Jul 18 '05 #3

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

Similar topics

72
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
10
by: John Machin | last post by:
Please consider the timings below, where a generator expression starts out slower than the equivalent list comprehension, and gets worse: >python -m timeit -s "orig=range(100000)"...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
12
by: Thomas Lotze | last post by:
Hi, I'm trying to figure out what is the most pythonic way to interact with a generator. The task I'm trying to accomplish is writing a PDF tokenizer, and I want to implement it as a Python...
2
by: Gary | last post by:
I am working with a report generator that is based on SQL Server 2000 and uses ASP as the UI. Basically we have a set of reports that end users can execute through a web browser. In general the...
2
by: UnixSlaxer | last post by:
Hello, - Is any body aware of a random workload/query generator such as the TPC-H query generator (QGEN) ? I am looking for a query generator that takes a schema as an input, and produces...
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...
41
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for...
1
by: neokosmos | last post by:
I've seen various generator-based microthread implementations online, but I've been wondering: has anyone used microthreads in this manner in a game environment? Note, I am emphatically *not*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.