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): - def listAppendTest(n):
-
aList = []
-
for i in range(n):
-
aList.append(i)
-
-
def listInitTest(n):
-
aList = [0] * n
-
for i in range(n):
-
aList[i] = 1
-
In a second module I use timeit to check this theory: - import timeit
-
-
test1ExecStr = \
-
"""from module1 import listAppendTest
-
listAppendTest(5)"""
-
-
t = timeit.Timer(test2ExecStr)
-
print t.timeit()
-
-
# 22.7464275488
-
-
test2ExecStr = \
-
"""from module1 import listInitTest
-
listInitTest(5)"""
-
-
t = timeit.Timer(test2ExecStr)
-
print t.timeit()
-
-
# 17.7503707873
Interesting results, no?
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.
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: - def __add__(self, other):
-
return Point(*[a+b for a,b in zip(self,other)])
Method 2: - def __add__(self, other):
-
return Point(self.x+other[0], self.y+other[1], self.z+other[2])
Which one should I use?
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?
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): - def listAppendTest(n):
-
aList = []
-
for i in range(n):
-
aList.append(i)
-
-
def listInitTest(n):
-
aList = [0] * n
-
for i in range(n):
-
aList[i] = 1
-
In a second module I use timeit to check this theory: - import timeit
-
-
test1ExecStr = \
-
"""from module1 import listAppendTest
-
listAppendTest(5)"""
-
-
t = timeit.Timer(test2ExecStr)
-
print t.timeit()
-
-
# 22.7464275488
-
-
test2ExecStr = \
-
"""from module1 import listInitTest
-
listInitTest(5)"""
-
-
t = timeit.Timer(test2ExecStr)
-
print t.timeit()
-
-
# 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. - import timeit
-
-
test1ExecStr = \
-
"""from test3 import listAppendTest"""
-
-
t = timeit.Timer(test1ExecStr)
-
print t.timeit()
-
-
# 8.945650355
-
-
test2ExecStr = \
-
"""from test3 import listInitTest"""
-
-
t = timeit.Timer(test2ExecStr)
-
print t.timeit()
-
-
# 8.87637339383
Sign in to post your reply or Sign up for a free account.
Similar topics
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;
...
|
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...
|
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...
|
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...
|
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...
|
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=
|
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
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |