[Tutor] Python2 speed vs Python1.5.2

Scott Ralph Comboni scott@zenplex.com
23 May 2001 14:46:44 -0400


Yes I should have done that in the first place.  I'm running Linux
RH7.1.  I'm still very much new at all this Python being my first
language.  Also Im sure there are plenty of better ways to accomplish
what this script does but here it is.
Anyway the reason for the script is we are trying to simulate
transaction loads on a large scale so I just wrote this to generate 1000
transactions.

#!/usr/bin/env python

# Created May 23, 2001
#
# Im sure there are plenty of ways to accomplish this.
# One using the Python xmllib.


import re
import whrandom

# Base SAN # change as needed
SAN = '222-2222-'

# A Working XML file.  I used this as a template.
xmlfile = open('xmltemplate','r').read()

# Create regular expressions.
# I'm sure most of this can be accomplished with one regex but..
# I have not spent enough time trying to figure it out.
isbn          = re.compile(r"<isbn>.+</isbn>")
recordid      = re.compile(r"<recordid>.+</recordid>")
ponumber      = re.compile(r"<ponumber>.+</ponumber>")
printingno    = re.compile(r"<printingno>.+</printingno>")
noterecordid  = re.compile(r"<noterecordid>.+</noterecordid>")
noteparentid  = re.compile(r"<noteparentid>.+</noteparentid>")
compparentid  = re.compile(r"<compparentid>.+</compparentid>")
transrecordid = re.compile(r"<transrecordid>.+</transrecordid>")

# Use range() to set the max number of transactions. Default is 1000.
# Using str to convert a int to string.
# whrandom to generate a randomo # for po, isbn ,printno.
for x in range(1000):
    xmlfile = recordid.sub('<recordid>' + SAN + str(x) + '</recordid>',
xmlfile)
    xmlfile = transrecordid.sub('<transrecordid>' + SAN + str(x) +
'</transrecordid>', xmlfile)
    xmlfile = noterecordid.sub('<noterecordid>' + SAN + str(x) +
'</noterecordid>', xmlfile)
    xmlfile = noteparentid.sub('<noteparentid>' + SAN + str(x) +
'</noteparentid>', xmlfile)
    xmlfile = compparentid.sub('<compparentid>' + SAN + str(x) +
'</compparentid>', xmlfile)
    xmlfile = ponumber.sub('<ponumber>' + str(whrandom.random()) +
'</ponumber>', xmlfile)
    xmlfile = isbn.sub('<isbn>' + str(whrandom.random()) + '</isbn>',
xmlfile)
    xmlfile = printingno.sub('<printingno>' + str(whrandom.random())
+'</printingno>', xmlfile)
    outputfile = open(SAN + str(x) + '.xml','w')
    outputfile.write(xmlfile)
    outputfile.close()
    print 'Creating transaction ', x, 'done'


Thanks Scott

On 23 May 2001 13:11:28 -0500, Rob Andrews wrote:
> 3;-> I just finished a small script that uses the re module for regular
> 3;-> expression processing.  It's a very simple script that
> 3;-> parses a text
> 3;-> file and does a re.sub then saves the new file out 1000
> 3;-> times. I ran the
> 3;-> script under Python2.0 and Python1.5.2 and found that
> 3;-> Python1.5.2 took
> 3;-> 4secs while 2.0 took 15secs..  Any answer why Python2.0 is slower?
> 3;->
> 3;-> Scott
> 3;->
> 
> Can you provide a little more info, such as which OS you are running on and
> maybe the source to the script?
> 
> Rob
> 
> Got Python?
> http://www.lowerstandard.com/python/pythonsource.html

-- 
Scott Comboni
president
Zenplex, Inc.
317 Madison Ave.
Suite 1500
New York NY, 10017
212.499.0668 ext2219
http://www.zenplex.com
http://www.zenplex.org
http://tambora.zenplex.org