[Tutor] Load Entire File into memory
Stefan Behnel
stefan_ml at behnel.de
Tue Nov 5 08:28:32 CET 2013
Amal Thomas, 04.11.2013 14:55:
> I have checked the execution time manually as well as I found it through my
> code. During execution of my code, at start, I stored my initial time(start
> time) to a variable and at the end calculated time taken to run the code =
> end time - start time. There was a significance difference in time.
You should make sure that there are no caching effects here. Your operating
system may have loaded the file into memory (assuming that you have enough
of that) after the first read and then served it from there when you ran
the second benchmark.
So, make sure you measure the time twice for both, preferably running both
benchmarks in reverse order the second time.
That being said, it's not impossible that f.readlines() is faster than
line-wise iteration, because it knows right from the start that it will
read the entire file, so it can optimise for it (didn't check if it
actually does, this might have changed in Py3.3, for example).
Stefan
More information about the Tutor
mailing list