[Tutor] Efficient programming questions. Tuples vs Lists; Custom Objects vs Lists.

Wesley Brooks wesbrooks at gmail.com
Mon Sep 25 10:08:13 CEST 2006


Dear Python-Tutor members,

I'm currently in the middle of re-writing a program for my research. I've
been using python for the past three years now, my first language since a
brief exposure to qbasic ten years ago. There are a couple of things I'm not
sure about which I'll need to clear up before I present my work, I hope you
can either help me or point me to literature / web reference which can help.
Most of these are issues relating to a mix of speed of execution for the
code, and scripting best practice.

Firstly tuples vs lists. I'm guessing that lists use more memory than tuples
as they provide more functions? Are they also more CPU intensive to use?
Currently if I'm not likely to add or remove Items I use a tuple (eg, a
coordinate in 3D space), but when I do I prefer using a list. This leads on
to another question: If you use an object many times, for instance a list,
does the interpreter remember that each new object is a list and when a
function is called on a list look at one section of memory which details the
list functions, or for each new object does it dedicate a new section of
memory to the functions of that object?

Secondly, a similar question to the first. A list object is something which
is in the standard python library. I guess in a CPython distribution that
this is C/C++ code being called by the python interpreter when the list
object is used? If so then this would imply that a list object would be
significantly quicker/less memory to use than an equivalent object scripted
in python. I'm currently using lists extensively to hold basic information
within objects with additional functions in the script to return information
about items within the list. My code would be a lot more elegant and easier
to read if I used custom objects for some of these but I'm worried they
would be much slower. Would it be appropriate to write a function that
inherited the methods of the List function? Would the new object retain the
speed and efficiency of the standard list object methods?

Lastly why can't python be compiled? I understand that there are certain
situations where it is preferable to leave the script readable or byte code
interpreted such as when programs are updated frequently over the net, or
are being distributed to computers with different operating systems. What
about situations where speed is critical? Is CPython's interpreter
effectively a C program that carries out C functions as requested in the
script? If so why is it not possible to have a program that reads in the
whole python script, translates it to C and compiles it? Is it simply that
the C functions are compiled already so carrying out a complete compile
would gain minimal increases in performance?


Thank you for your time and help.

Yours Faithfully,

Wesley Brooks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060925/19a4a549/attachment.html 


More information about the Tutor mailing list