[Tutor] Simple Question...

R. Alan Monroe amonroe at columbus.rr.com
Sun Oct 17 05:57:22 CEST 2004


> the file. It must be capable of returning the first line, the last
> line, and anything in between.

Your fortune file was a bit hard to debug with since many lines were
blank. I'll attach the test file I ended up using.

Repeatedly opening/closing the file in question is murder, in a tight
loop. Doing an os.path.getsize() is pretty slow too. If you do both of
those on the outside of the tight loop, it really speeds up.


C:\coding\python>python randomline.py
4.13174830879 (open/close the file each time)
2.369336885 (open the file outside the loop)
0.68840117948 (open and get size outside the loop)

Times given are for 20000 iterations on a 2.4ghz laptop.

Alan
-------------- next part --------------
1ldkfg
2aasd
3srgn
4der
5zd
6xf
7dt
8rs
9yi
10dfg
11xr
12m
13dth
14js
15gth
16dxfgn
17drn
18hs
19g
20nd
21thm
22dth
23nxd
24g
25hr
26dhj
27dsr
28gk
29dfty
30ur
31dftm
32d
33gx
34fn
35d
36thm
37dr
38hfm
39r
40hs
41etjdtrhdj
42d
43thm
44dm
45thd,dthkmd
46dhn
47dthmsdrt
48ncvbn c
-------------- next part --------------
import os.path
import random
import time


def getrandomline1(filename):
    size = os.path.getsize(filename)
    testfile = open(filename, 'r')
    firstline = testfile.readline()
    secondline = testfile.readline()

    retval=""
    while len(retval) < 1:
        randpos = random.randint(1, size)
        if randpos <= len(firstline):
            retval = firstline
        else:
            testfile.seek(randpos)
            if randpos <= len(firstline) + len(secondline):
                testfile.seek(1-len(secondline), 1)
            testfile.readline()
            retval = testfile.readline()
        
    testfile.close()
    return (randpos, retval.strip())

def getrandomline2(filename, testfile):
    size = os.path.getsize(filename)
    testfile.seek(0)
    firstline = testfile.readline()
    secondline = testfile.readline()

    retval=""
    while len(retval) < 1:
        randpos = random.randint(1, size)
        if randpos <= len(firstline):
            retval = firstline
        else:
            testfile.seek(randpos)
            if randpos <= len(firstline) + len(secondline):
                testfile.seek(1-len(secondline), 1)
            testfile.readline()
            retval = testfile.readline()
        
    return (randpos, retval.strip())

def getrandomline3(filename, testfile, size):
    testfile.seek(0)
    firstline = testfile.readline()
    secondline = testfile.readline()

    retval=""
    while len(retval) < 1:
        randpos = random.randint(1, size)
        if randpos <= len(firstline):
            retval = firstline
        else:
            testfile.seek(randpos)
            if randpos <= len(firstline) + len(secondline):
                testfile.seek(1-len(secondline), 1)
            testfile.readline()
            retval = testfile.readline()
        
    return (randpos, retval.strip())


starttime=time.clock()
for x in range(20000):
    randline=getrandomline1('test.txt')
endtime=time.clock()
print endtime-starttime


filehandle = open('test.txt', 'r')
starttime=time.clock()
for x in range(20000):
    randline=getrandomline2('test.txt', filehandle)
endtime=time.clock()
filehandle.close()
print endtime-starttime


filehandle = open('test.txt', 'r')
filesize = os.path.getsize('test.txt')
starttime=time.clock()
for x in range(20000):
    randline=getrandomline3('test.txt', filehandle, filesize)
endtime=time.clock()
filehandle.close()
print endtime-starttime


More information about the Tutor mailing list