"Napravio sam da se vidi poslednjih deset lniija."
Dobro je to ako imaš mali fajl, npr. par MB ili malo više, a ša ako recimo imaš fajl od par GB ili više. Na primer:
Code:
du -h log.txt
1.6G log.txt
# Komanda tail
$ time tail log.txt
tail log.txt 0.00s user 0.00s system 85% cpu 0.002 total
# Tvoj tail program
$ time ./tail.py
./tail.py 8.74s user 0.40s system 99% cpu 9.139 total
Problem je što tvoj kod prolazi kroz ceo fajl da bi prikazao poslednjih 10 linija, zar nije bolje da krene od poslednje linije u fajlu.
Da definišeš poziciju, pogledaj seek funkciju:
Citat:
seek(target, whence=0, /) method of _io.BufferedReader instance
Change stream position.
Change the stream position to the given byte offset. The offset is
interpreted relative to the position indicated by whence. Values
for whence are:
* 0 -- start of stream (the default); offset should be zero or positive
* 1 -- current stream position; offset may be negative
* 2 -- end of stream; offset is usually negative
Return the new absolute position.
Na primer:
Code:
# Krece od poslednje linije
position = fh.seek(0, 2)
# ....
$ time ./tail.py
./tail.py 0.02s user 0.00s system 97% cpu 0.025 total
Mnogo bolje zar ne? Sad ti samo ostaje da implementiraš ostatak koda.