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

Initialize lists; here's why

bartonc
6,596 Expert 4TB
Most other programming languages, as well as Python extension packages require arrays to be initialized to a particular size. Well, it turns out that there is an advantage to doing this with native python lists, too (that is, of course, if you know the size of the list beforehand).

I wrote two tests in a module (call it module1):
Expand|Select|Wrap|Line Numbers
  1. def listAppendTest(n):
  2.     aList = []
  3.     for i in range(n):
  4.         aList.append(i)
  5.  
  6. def listInitTest(n):
  7.     aList = [0] * n
  8.     for i in range(n):
  9.         aList[i] = 1
  10.  
In a second module I use timeit to check this theory:
Expand|Select|Wrap|Line Numbers
  1. import timeit
  2.  
  3. test1ExecStr = \
  4. """from module1 import listAppendTest
  5. listAppendTest(5)"""
  6.  
  7. t = timeit.Timer(test2ExecStr)
  8. print t.timeit()
  9.  
  10. # 22.7464275488
  11.  
  12. test2ExecStr = \
  13. """from module1 import listInitTest
  14. listInitTest(5)"""
  15.  
  16. t = timeit.Timer(test2ExecStr)
  17. print t.timeit()
  18.  
  19. # 17.7503707873
Interesting results, no?
Aug 9 '07 #1
4 11146
Your testing only five times? I doubt that is statistically sound. What happens when you try 1000?

In any case trying to micromanage performance like this is really the wrong way to programme. First, find if your app is not fast enough, then profile, then fix the bottleneck.

Remember the first rule of optimisation: Don't.
Aug 9 '07 #2
bvdet
2,851 Expert Mod 2GB
Your testing only five times? I doubt that is statistically sound. What happens when you try 1000?

In any case trying to micromanage performance like this is really the wrong way to programme. First, find if your app is not fast enough, then profile, then fix the bottleneck.

Remember the first rule of optimisation: Don't.
By my recollection, timeit() defaults to 1000000 iterations.

I routinely optimize as I program where possible. If I know a one method is more efficient than another, I use the most efficient, unless additional functionality is required. Occasionally I use timeit() just so I will know.

Would you use string concatenation repeatedly to build long strings or the string join method? Here's another example:

Method 1:
Expand|Select|Wrap|Line Numbers
  1. def __add__(self, other):
  2.         return Point(*[a+b for a,b in zip(self,other)])
Method 2:
Expand|Select|Wrap|Line Numbers
  1. def __add__(self, other):
  2.         return Point(self.x+other[0], self.y+other[1], self.z+other[2])
Which one should I use?
Aug 9 '07 #3
bartonc
6,596 Expert 4TB
Your testing only five times? I doubt that is statistically sound. What happens when you try 1000?

In any case trying to micromanage performance like this is really the wrong way to programme. First, find if your app is not fast enough, then profile, then fix the bottleneck.

Remember the first rule of optimisation: Don't.
How 'bout 5 million times?
Aug 10 '07 #4
bartonc
6,596 Expert 4TB
Most other programming languages, as well as Python extension packages require arrays to be initialized to a particular size. Well, it turns out that there is an advantage to doing this with native python lists, too (that is, of course, if you know the size of the list beforehand).

I wrote two tests in a module (call it module1):
Expand|Select|Wrap|Line Numbers
  1. def listAppendTest(n):
  2.     aList = []
  3.     for i in range(n):
  4.         aList.append(i)
  5.  
  6. def listInitTest(n):
  7.     aList = [0] * n
  8.     for i in range(n):
  9.         aList[i] = 1
  10.  
In a second module I use timeit to check this theory:
Expand|Select|Wrap|Line Numbers
  1. import timeit
  2.  
  3. test1ExecStr = \
  4. """from module1 import listAppendTest
  5. listAppendTest(5)"""
  6.  
  7. t = timeit.Timer(test2ExecStr)
  8. print t.timeit()
  9.  
  10. # 22.7464275488
  11.  
  12. test2ExecStr = \
  13. """from module1 import listInitTest
  14. listInitTest(5)"""
  15.  
  16. t = timeit.Timer(test2ExecStr)
  17. print t.timeit()
  18.  
  19. # 17.7503707873
Interesting results, no?
Interestingly, (and this is something that I've been meaning to do for a long time) the import is taking a significant amount of time.
Expand|Select|Wrap|Line Numbers
  1. import timeit
  2.  
  3. test1ExecStr = \
  4. """from test3 import listAppendTest"""
  5.  
  6. t = timeit.Timer(test1ExecStr)
  7. print t.timeit()
  8.  
  9. # 8.945650355
  10.  
  11. test2ExecStr = \
  12. """from test3 import listInitTest"""
  13.  
  14. t = timeit.Timer(test2ExecStr)
  15. print t.timeit()
  16.  
  17. # 8.87637339383
Aug 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matjaz | last post by:
Dear all, I have run into a problem using python lists and sequence protocol. The first code snippet uses explicit list operations and works fine. PyObject *argseq, *ov; int i, v, len; ...
1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
11
by: hana1 | last post by:
Hello guys, I just have a quick question about C++. I am moving from Java to C++. And as many of Java users know, all linked list and trees are already implemented and you just have to instantiate...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
1
by: pasa_1 | last post by:
Can someone clarify few items from FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 ' Should my constructors use "initialization lists" or "assignment"?' a. This might happen...
6
by: artistlikeu | last post by:
hi All, i have a list and putting an object say "traffic light" in it. nrTrafficLight=2 objectsList= objectsList.append('TrafficLight') objTypeList=
10
by: Anton Vredegoor | last post by:
Python's sorting algorithm takes advantage of preexisting order in a sequence: #sort_test.py import random import time def test(): n = 1000 k = 2**28
4
by: swtsvn | last post by:
hi all iam having two lists list<float*scan; list<float*AET; and if i call list<float*>::iterator i; the scan array of list pointers is getting referenced by the iterator how do i access...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.