[Edu-sig] How to Improve code performance

Seth David Schoen schoen@loyalty.org
Mon, 29 Oct 2001 22:43:12 -0800


Seth David Schoen writes:

> If all you do with tab is print it out, you don't need to do all
> of those string operations, or function calls:
> 
> print "Content-type: text/html"
> print "<table align='center' border='1'>"
> for x in result:
> 	print "<tr>"
> 	for item in x:
> 		print "<td>"+str(item)+"</td>"
> 	print "</tr>"
> print "</table>"
> 
> Printing output right away would have lower overhead and use less
> memory.

This can be a big deal for efficiency in a very high level language.
Building large or complex data structures could have very high
overhead; for example, dynamic allocation of space for strings can be
a real problem if you do it over and over again hundreds of thousands
of times!

Anyway, this also has me wondering about the relative speed of
different Python iteration idioms (for, while, map, recursion, and now
list comprehensions).  They're not all applicable to the same
problems, but where there is overlap, which are faster than others?

-- 
Seth David Schoen <schoen@loyalty.org> | Its really terrible when FBI arrested
Temp.  http://www.loyalty.org/~schoen/ | hacker, who visited USA with peacefull
down:  http://www.loyalty.org/   (CAF) | mission -- to share his knowledge with
     http://www.freesklyarov.org/      | american nation.  (Ilya V. Vasilyev)