Question on periods in strings

Philip Bloom pbloom at crystald.com
Wed Mar 11 21:42:45 EDT 2009


Thanks for the welcome :)

You're right.  Here's with the missed line (I was cutting out commented parts).  Hopefully these are all cut/paste-able.

#test A
#runs in 5.8 seconds.
from datetime import datetime
testvar2='9a00'
startTime = datetime.now()
filehandle=open('testwriting.txt','w')
for var in range(10000000):
    filehandle.write(testvar2)
filehandle.close()
print (datetime.now() - startTime)


#test B
#runs in 10.9 seconds.
from datetime import datetime
testvar2='9.00'
startTime = datetime.now()
filehandle=open('testwriting.txt','w')
for var in range(10000000):
    filehandle.write(testvar2)
filehandle.close()
print (datetime.now() - startTime)

I do use the same filename, but I've run the tests in different orders and it's made no difference.  Repeatedly running the same test results in the same numbers with only minor fluctuations (as would be expected from cache issues).  Ten runs in a row of Test B all result in about 11 seconds each.  Ten runs in a row of Test A all result in about 6 seconds each.

The range is not actually a meaningful adjustment as the time results are identical switching out xrange (as I believe they should be since in 2.6 range maps to xrange for the most part according to some of the docs).  

#Test C
#runs in 8.9 seconds.
from datetime import datetime
testvar2='9.00'
startTime = datetime.now()
join=[]
filehandle=open('testwriting.txt','w')
for var in range(10000000):
    join.append(testvar2)
"".join(join)
print (datetime.now() - startTime) #3.01 seconds   
filehandle.write("".join(join))
filehandle.close()
print (datetime.now() - startTime) #8.9 seconds.

#Test D
#runs in 3.8 seconds.
from datetime import datetime
testvar2='9a00'
startTime = datetime.now()
join=[]
filehandle=open('testwriting.txt','w')
for var in range(10000000):
    join.append(testvar2)
"".join(join)
print (datetime.now() - startTime) #3.09 seconds   
filehandle.write("".join(join))
filehandle.close()
print (datetime.now() - startTime) #3.87 seconds.

This is a variation that shows it more noticeably.  I do an extra join to demonstrate that's not taking the time.  Effectively nothing is really different it seems like other than the period.  It's all intentionally small python code since it originated from just seeing what some variations on file writes might differ in scaling.
 
-----Original Message-----
From: python-list-bounces+pbloom=crystald.com at python.org [mailto:python-list-bounces+pbloom=crystald.com at python.org] On Behalf Of Gabriel Genellina
Sent: Wednesday, March 11, 2009 6:17 PM
To: python-list at python.org
Subject: Re: Question on periods in strings

En Wed, 11 Mar 2009 22:35:22 -0200, Philip Bloom <pbloom at crystald.com>  
escribió:

> Hello, this is my first time posting to the list, but my curiosity here
> is great.

Welcome!

> I was randomly toying with file writes and I ran into something that
> seemed quite odd to me.  When a period is in a string, file write takes
> about double the time.  I saw similar things with newlines, but I
> figured that was because it was doing a lookup on the escape-d n.  All
> other symbols seem to not have this problem, so I'm curious what's
> special about periods that they take so much longer to write to file.

I doubt the period is actually the culprit. There are lots of other  
variables - like disk and memory fragmentation, cpu load, ...
You omitted part of your code: do you use the same filename on both tests?  
(Note that starting with an empty file is not the same as using a  
preallocated file).
Also, do you always run both scripts in the same order? (maybe "the  
second" always runs faster, no matter which one is it).
Also note that you're measuring the time to allocate a list containing ten  
million integer objects (the range() call). xrange is a less perturbing  
option.

-- 
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________



More information about the Python-list mailing list