On 3/20/15 3:50 AM, 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's not elided to the best of my knowledge. But it's reference counted
so it goes from 0 to 1 and back. A simple caching allocator will have no
trouble with this pattern. -- Andrei