473,226 Members | 1,534 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,226 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 11137
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

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.