optimization

Rob W. W. Hooft rob at hooft.net
Mon May 15 03:01:37 EDT 2000


>>>>> "EH" == Eric Hagemann <ehagemann at home.com> writes:

 EH> Is it faster to split a string into a tuple with individual
 EH> elements

 >>> a= "as the end" 
 >>> (x,y,z) = string.split(a," ")

 EH> or as a list

 >>> q = string.split(a," " )

 EH> where following accesses would entail q[0]='as' etc.

Why does it need to be the fastest?

 EH> Is is faster to index through a list

 >>>> a=[1,2,3,4.....]

 >>>> for i in range(4): a[i].....

 EH> or

 >>>> for x in a: .......

Why does it need to be the fastest?

 EH> I am looking in general to produce
 EH> quick code and am looking for coding techniques that are best
 EH> utilize the language.

The best python techniques are those that make the code the most
readable. So: if you are writing a simple command interpreter:

    (command,source,destination)=string.split(commandline)

(Note: no x,y,z)

And if you need to iterate over all elements of a list:

    for commandline in script:
        do()

The general idea of optimizing python is: Write your program. Do not
focus on speed, but on readability and maintainability. If it is too
slow: optimize the inner loop while documenting it very well. If it is
still too slow, write a C routine to replace the inner loop.

See also: 
   http://www.python.org/doc/essays/ppt/hp-training/sld039.htm
   http://www.python.org/doc/essays/list2str.html

-- 
=====   rob at hooft.net          http://www.xs4all.nl/~hooft/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========




More information about the Python-list mailing list