Dear all,
I tried the test program below. My interest is to examine timing
differences between insert vs. append & reverse for a list. My results
on my XP Python 2.3.4 are as follows:
time_reverse 0.889999389648
time_insert 15.7750005722
Over multiple runs ... the time taken to insert at the head of a list,
vs. the time taken to append to a list and then reverse it is
typically 16 or 17 times longer.
I would have expected the insert operation to be faster than the
combined append & reverse operations. Is this behaviour surprising, or
is there a good reason why there is this large performance difference?
Thanks,
John
from time import time
def test():
time_reverse =0.0
time_insert =0.0
for lindx in xrange(100):
tmp1 =[]
time_reverse -=time()
for indx in xrange(10000):
tmp1.append(ind x)
tmp1.reverse()
time_reverse +=time()
tmp2 =[]
time_insert -=time()
for indx in xrange(10000):
tmp2.insert(0, indx)
time_insert +=time()
assert tmp1==tmp2
print "time_rever se ", time_reverse
print "time_inser t ", time_insert
test() 19 11292 jo**********@ya hoo.com (John Keeling) wrote in
news:35******** *************** ***@posting.goo gle.com: I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows: time_reverse 0.889999389648 time_insert 15.7750005722 Over multiple runs ... the time taken to insert at the head of a list, vs. the time taken to append to a list and then reverse it is typically 16 or 17 times longer. I would have expected the insert operation to be faster than the combined append & reverse operations. Is this behaviour surprising, or is there a good reason why there is this large performance difference?
A list is implemented by an array of pointers to the objects it contains.
Every time you call 'insert(0, indx)', all of the pointers already in the
list have to be moved up once position before the new one can be inserted
at the beginning.
When you call 'append(indx)' the pointers only have to be copied if there
isn't enough space in the currently allocated block for the new element. If
there is space then there is no need to copy the existing elements, just
put the new element on the end and update the length field. Whenever a new
block does have to be allocated that particular append will be no faster
than an insert, but some extra space will be allocated just in case you do
wish to extend the list further.
If you expected insert to be faster, perhaps you thought that Python used a
linked-list implementation. It doesn't do this, because in practice (for
most applications) a list based implementation gives better performance.
John Keeling wrote: I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list.
But what you are measuring is the difference between
one particular insert (where the index is 0) and the
combination of two other builtin operators that (in this
particular case) have the same effect.
While your observation makes a good point, in the end
we should pleased not surprised when we find ways to
speed up these specific cases.
Istvan.
"John Keeling" <jo**********@y ahoo.com> wrote in message
news:35******** *************** ***@posting.goo gle.com... Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows: time_reverse 0.889999389648 time_insert 15.7750005722 Over multiple runs ... the time taken to insert at the head of a list, vs. the time taken to append to a list and then reverse it is typically 16 or 17 times longer.
Shouldn't that really be insert vs. reverse & append & reverse. :-)
Duncan
Duncan, A list is implemented by an array of pointers to the objects it contains.
That explains it.
If you expected insert to be faster, perhaps you thought that Python used a linked-list implementation. It doesn't do this,
Yes, that is how I was thinking. It is interesting to see how
functions may perform very differently from how we may initially
expect. I've also noted an approximate two time difference between
"del list[index] and list.pop(index) ". I know that they do different
things (the return value from pop), however in many places my code
uses pop and ignores the return value when it would be faster to use
del. I am making changes based on these observations now.
Thanks,
John
Duncan, A list is implemented by an array of pointers to the objects it contains.
That explains it.
If you expected insert to be faster, perhaps you thought that Python used a linked-list implementation. It doesn't do this,
Yes, that is how I was thinking. It is interesting to see how
functions may perform very differently from how we may initially
expect. I've also noted an approximate two time difference between
"del list[index] and list.pop(index) ". I know that they do different
things (the return value from pop), however in many places my code
uses pop and ignores the return value when it would be faster to use
del. I am making changes based on these observations now.
Thanks,
John
"John Keeling" <jo**********@y ahoo.com> wrote in message
news:35******** *************** ***@posting.goo gle.com... expect. I've also noted an approximate two time difference between "del list[index] and list.pop(index) ".
Here is one way to get some insight into such things: def ldel(lisp, index):
.... del lisp[index]
.... def lpop(lisp, index):
.... return lisp.pop(index)
.... import dis dis.dis(ldel)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_FAST 0 (lisp)
9 LOAD_FAST 1 (index)
12 DELETE_SUBSCR
13 LOAD_CONST 0 (None)
16 RETURN_VALUE dis.dis(lpop)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_FAST 0 (lisp)
9 LOAD_ATTR 1 (pop)
12 LOAD_FAST 1 (index)
15 CALL_FUNCTION 1
18 RETURN_VALUE
19 LOAD_CONST 0 (None)
22 RETURN_VALUE
You can be pretty sure that the specific opcode DELETE_SUBSCR (ipt) is
faster than the generic CALL_FUNCTION, which will eventually call (I
presume) the same C code. (The extra attribute lookup and load take some
extra time too, but the call should be the main culprit).
Terry J. Reedy
It is so cool to be able to get the disassembly
so easily.... thanks v. much Terry.
Cheers,
John import dis dis.dis(ldel)
0 SET_LINENO 1
3 SET_LINENO 2 6 LOAD_FAST 0 (lisp) 9 LOAD_FAST 1 (index) 12 DELETE_SUBSCR 13 LOAD_CONST 0 (None) 16 RETURN_VALUE
Duncan Booth wrote: A list is implemented by an array of pointers to the objects it contains.
Every time you call 'insert(0, indx)', all of the pointers already in the list have to be moved up once position before the new one can be inserted at the beginning.
When you call 'append(indx)' the pointers only have to be copied if there isn't enough space in the currently allocated block for the new element. If there is space then there is no need to copy the existing elements, just put the new element on the end and update the length field. Whenever a new block does have to be allocated that particular append will be no faster than an insert, but some extra space will be allocated just in case you do wish to extend the list further.
If you expected insert to be faster, perhaps you thought that Python used a linked-list implementation. It doesn't do this, because in practice (for most applications) a [array] based implementation gives better performance.
True, but an array implementation can easily support amortized
constant-time insert/delete at *either* end (without breaking
constant-time indexing). The same trick of keeping extra space
at the tail end can work at the head; it requires keeping one
additional offset to show where the occupied cells start.
--
--Bryan
"John Keeling" <jo**********@y ahoo.com> wrote in message
news:35******** *************** ***@posting.goo gle.com... It is so cool to be able to get the disassembly so easily.... thanks v. much Terry.
Since byte code is not (typically) written by hand, the symbolic names can
be long enough to be mostly self-explanatory, rather than short and crytic.
If any are not, for you, the Lib Manual dis module section has subsection
with all codes explained.
TJR This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Timo |
last post by:
When content is transferred from a hidden IFRAME (which has fetched data
from a database) to a DIV in the main document, how can a script determine
that the DIV has been completely populated before it acts upon the data?
if (myIFRAME.document.readyState=='complete')
myDIV.insertAdjacentHTML("afterBegin",
myIFRAME.window.document.getElementById('data') )
// ...
// convert text in myDIV to uppercase
// code here may execute before...
|
by: yaffa |
last post by:
dear folks,
i'm trying to append a semicolon to my addr string and am using the
syntax below. for some reason the added on of the ; doesn't work.
when i print it out later on it only shows the original value of addr.
addr = incident.findNextSibling('td')
addr.append('%s;')
thanks
|
by: Bart |
last post by:
Hi,
I receive an utf8 character from a database, like 田 (Japanese
Character, style: &#XXXXX).
How can I visualize the Japanese character on my application? I have found
the class System.Text.Encoding, but the input looks like \uXXXX. I don't
know how to do.
Thank you,
|
by: Tee |
last post by:
String Builder & String, what's the difference.
and when to use which ?
Thanks.
|
by: Bob Alston |
last post by:
I have a routine to copy data to new versions of my app via insert into
sql statements. Unfortunately, due to evolution of my app, sometimes
the new version has more restrictive editing than an older version that
I am updating. Thus I get this message. It tells me only how many
records have errors, not which errors or which records.
Anyone have a nice solution to identifying the specific records involved?
Maybe even the specific...
| |
by: Steven D'Aprano |
last post by:
The timeit module is ideal for measuring small code snippets; I want to
measure large function objects.
Because the timeit module takes the code snippet argument as a string, it
is quite handy to use from the command line, but it is less convenient for
timing large pieces of code or when working in the interactive
interpreter. E.g. variations on this *don't* work:
$ python
Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
|
by: ak1dnar |
last post by:
There is a Error getting while i am entering records using this jsp file.
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="../Connections/conn.jsp" %>
<%
// *** Edit Operations: declare variables
// set the form action variable
String MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
|
by: John Fisher |
last post by:
I am working on a framework for data acquisition in Python 2.5, am
trying to get a structure going more like this:
mark start time
start event
event finishes
count time until next interval
start second event…
|
by: billa856 |
last post by:
Hi,
I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date.
I am using "" to insert Null in that column(ShipDate) but it shows warning that
customer can't append all the records in the append query.
Customer set 1 field(s) to Null due to a type conersion failure,and it didn't add 0 record(s) to the table due to key violation, 0 record(s) due to lock...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |