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

Few questions

Hello, I have few more things to say/ask (left from a discussion in
another Python Newsgroup).

Is it possibile (and useful) to write few small sub-sections of the
Python interpreter in Assembly for Pentium (III/IV)/AMD, to speed up
the interpreter for Win/Linux boxes running on those CPUs? (Such parts
don't replace the C versions, kept for compatibilty).
I think the HLA (High Level Assembly) language can be fit for this
purpose, it's a cute language:
http://webster.cs.ucr.edu/AsmTools/HLA/index.html

--------

I've done a little comparison of the speed of Python lists and arrays:

# speed_test.py
from time import clock
import sys

def array_test():
from array import array
v = array("l", [0] * n)
t = clock()
for i in xrange(len(v)):
v[i] = i
print "Timing:", round(clock()-t,3), "s"

def list_test():
v = [0] * n
t = clock()
for i in xrange(len(v)):
v[i] = i
print "Timing:", round(clock()-t,3), "s"

n= 3*10**6
if str(sys.argv[1]) == "1":
print "List test, n =", str(n) + ":"
list_test()
else:
print "Array test, n =", str(n) + ":"
array_test()
On a old Win2K PC it gives:
C:\py>speed_test 1
List test, n = 3000000:
Timing: 2.804 s

C:\py>speed_test 2
Array test, n = 3000000:
Timing: 3.521 s
Python lists are arrays of pointers to objects, I think (a test shows
that here they use about 16 bytes for every number).
And the Python Arrays are packed: every number here uses 4 bytes.
Why do lists are faster here?

------

Memory cleaning: in the last script I've added some calls to a Win
version of the small "pslist" program, and I've put a "del v" command
after the timings. And I've seen:

C:\py>speed_test 1
List test, n = 3000000:
1) Process size: 1408 KB.
2) Process size: 48928 KB.
3) Process size: 37204 KB.

C:\py>speed_test 2
Array test, n = 3000000:
1) Process size: 1416 KB.
2) Process size: 13152 KB.
3) Process size: 1416 KB.

1 is at the start of the script before v creation, 2 is after its
inizialization loop, and 3 is after the "del v" command, like this:

used_mem(1)
from array import array
v = array("l", [0] * n)
for i in xrange(len(v)):
v[i] = i
used_mem(2)
del v
used_mem(3)

The garbage collector removes at once the array (this is easy, it's
just a lump of memory with little extra things), but the memory used
by the list isn't free even a little time later. (I think that to
understand how/when such such garbage collector works, I have to read
the Python C sources...)

Thank you,
bearophile
Jul 18 '05 #1
2 1336

be************@lycos.com (bearophile) wrote:

Hello, I have few more things to say/ask (left from a discussion in
another Python Newsgroup).

Is it possibile (and useful) to write few small sub-sections of the
Python interpreter in Assembly for Pentium (III/IV)/AMD, to speed up
the interpreter for Win/Linux boxes running on those CPUs? (Such parts
don't replace the C versions, kept for compatibilty).
I think the HLA (High Level Assembly) language can be fit for this
purpose, it's a cute language:
http://webster.cs.ucr.edu/AsmTools/HLA/index.html
It may or may not be useful or faster to implement portions of the
Python interpreter in assembly. Generally assembly has performance
benefits and penalties per processor that are hard to understand.

I would be willing to wager that time would be better spent checking out
the different compile-time options for the interpreter, as C optimizes
fairly well.
I've done a little comparison of the speed of Python lists and arrays: [snip code] On a old Win2K PC it gives:
C:\py>speed_test 1
List test, n = 3000000:
Timing: 2.804 s

C:\py>speed_test 2
Array test, n = 3000000:
Timing: 3.521 s
Python lists are arrays of pointers to objects, I think (a test shows
that here they use about 16 bytes for every number).
And the Python Arrays are packed: every number here uses 4 bytes.
Why do lists are faster here?
Crucial observation:
Lists are indeed arrays of pointers that point to 'int objects'.
Arrays (of integers) are arrays of actual stored x-bit integers (where x
is 32 in this case).

In order to write to an array the value of a standard Python integer,
one must look into the 16 byte Python integer to copy the proper 4 bytes
into the array, do bounds checking, etc.

In order to write to a list the value of a standard Python integer, a
pointer copy is sufficient.
Lists win because it is a pointer copy as opposed to an struct lookup
and value copy with bounds checking.

Memory cleaning: in the last script I've added some calls to a Win
version of the small "pslist" program, and I've put a "del v" command
after the timings. And I've seen:

C:\py>speed_test 1
List test, n = 3000000:
1) Process size: 1408 KB.
2) Process size: 48928 KB.
3) Process size: 37204 KB.

C:\py>speed_test 2
Array test, n = 3000000:
1) Process size: 1416 KB.
2) Process size: 13152 KB.
3) Process size: 1416 KB.

1 is at the start of the script before v creation, 2 is after its
inizialization loop, and 3 is after the "del v" command, like this:

used_mem(1)
from array import array
v = array("l", [0] * n)
for i in xrange(len(v)):
v[i] = i
used_mem(2)
del v
used_mem(3)

The garbage collector removes at once the array (this is easy, it's
just a lump of memory with little extra things), but the memory used
by the list isn't free even a little time later. (I think that to
understand how/when such such garbage collector works, I have to read
the Python C sources...)

Python arrays (from the array module) are allocated as a block, and the
values of integers are stored within. Because everything is all nice
and contained, it can be easily freed (just like C arrays).

With Python lists, certainly the pointers to the integer objects are
easily allocated and freed, and the integer objects themselves sit in
the integer free-list.

Now, obviously a bunch of those entries aren't being used after one
deletes the big list of integer, so why isn't it being freed? Due to
the semantics of the free list (you can't reorganize the integers on the
free list, etc.), it cannot be reduced in size.

- Josiah

Jul 18 '05 #2
bearophile wrote:

but the memory used by the list isn't free even a little time later.


Moreover won't ever be freed (from the operating system's view)
until the program ends. That's how C works and has nothing to do
with the kind of object that was allocated for. The only thing that
a free() operation is required to do is to make the freed
memory available for allocation within the same program.

Istvan.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.