[Tutor] reading random line from a file
Luke Paireepinart
rabidpoobear at gmail.com
Sun Jul 15 09:00:30 CEST 2007
max baseman wrote:
> cool thanks
>
> oh for performance eventualy i would like the file to contain many quotes
Using readlines isn't exactly going to cause a performance bottleneck.
I used the following code
#make the file.py
f = file("temp.txt","w")
x = 100000
while x > 0:
f.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n")
x -= 1
f.close()
#-------
this creates a file with a whole lot of lines of 'a's.
100,000 lines, to be exact, and 4,200,000 bytes.
In other words, this is a fair approximation for if you had, say, 25,000
quotes (since your quotes are likely to be, on average, longer than the
amount of 'a's I used.)
I think you'll agree that that's quite a few quotes.
Now how long does it take to use readlines() on this file?
#test performance.py
import timeit
string = "f = file('temp.txt','r');f.readlines();f.close()"
temp = timeit.Timer(stmt=string)
print "1000 iterations took: " + str(temp.timeit(1000))
#-----
what this code does is opens, reads all the text of the file, and closes
the file.
We call timeit with 1000 as the argument, so it repeats this process
1000 times.
The output of this program on my machine is:
1000 iterations took: 51.0771701431
In other words, if you have 25,000 quotes, you could read all of them
into memory in 51.07717/1000 (approximately)
or 0.05107 seconds. And I'm skeptical that you would even have that
many quotes.
So, like i said before, I doubt this will cause any significant
performance problem in pretty much any normal situation.
Also, by the way - please reply to me on-list so that others get the
benefit of our conversations.
-Luke
More information about the Tutor
mailing list