On Friday, 20 March 2015 at 10:50:44 UTC, Walter Bright wrote:
On 3/20/2015 12:42 AM, Paulo Pinto wrote:
On Friday, 20 March 2015 at 05:17:11 UTC, Walter Bright wrote:
Sigh. The Python version:
-----------
import sys
if __name__ == "__main__":
if (len(sys.argv) < 2):
sys.exit()
infile = open(sys.argv[1])
linect = 0
for line in infile:
linect += 1
print "There are %d lines" % linect
----------
does not allocate memory. The splitLines() version:
[...] cutted
Of course it does allocate memory.
Python's "for...in" makes use of iterators and generators,
already there you
have some allocations going around.
Not only that, there is one string being allocated for each
line in the file
being read, even if it isn't used.
Since 'line' is never referred to again after constructed, even
a simple optimizer could elide it.
It would be easy to test - accumulate the lines in an array,
and check the times.
Which the default Python implementation doesn't have, hence my
comment.
Also even if it did have one, it cannot elide it as it cannot
guarantee the semantics of the generators/iterators side effects
will be kept.
--
Paulo