# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import mmap
import time
import sys
import platform

SIZE = 1000000000

def test(fnm):
    print('%s / %s' % (sys.version, platform.platform()))
    with open(fnm, 'r+b') as f:
        t1 = time.time()
        mm = mmap.mmap(f.fileno(), SIZE, access=mmap.ACCESS_READ)
        n = 0
        for ix in range(SIZE):
            mm.seek(ix)
            b = mm.read_byte()
            if b == ord('\n') or b == '\n': # little trick - the first works with Python 3.x - the second works with Python 2.x
                n = n + 1
        print('%d lines' % (n))
        t2 = time.time()
        print('Map file to memory : %d ms' % ((t2 - t1) * 1000.0))

for i in range(3):
    test(sys.argv[1])
