in reply to Re^4: Loading a part of the file to array using Tie::File
in thread Loading a part of the file to array using Tie::File
Or do you see any serious use cases for it?
Dunno, I think that for random access of small files (say, maybe, under a megabyte) in situations where performance is not critical, the ease of implementation can still outweigh the cost. On the other hand, at least in my experience such files are rare. For example, when inserting lines somewhere, one usually has to scan the file to locate the insertion point anyway, so in such cases a while(<>) loop would still feel more natural to me than a linear search in an array. It's maybe a nice module to show off some of the power (Update: as in expressiveness / TIMTOWTDI, not speed) of Perl to newcomers, although then one might cause the problem of "if all you have is a hammer, everything looks like a nail".
Of course ikegami has a good point. There is a huge difference between reading even a ~1MB file into in array, vs. reading it with Tie::File:
$ cp -L /usr/share/dict/words /tmp/test.txt $ wc -l /tmp/test.txt 99132 /tmp/test.txt $ du -sh /tmp/test.txt 920K /tmp/test.txt $ time perl -MTie::File -e 'open F, "/tmp/test.txt" or die; print `ps -orss $$`; my @x = <F>; print `ps -orss $$`' RSS 7408 RSS 23604 real 0m0.042s user 0m0.024s sys 0m0.012s $ time perl -MTie::File -e 'tie my @a, "Tie::File", "/tmp/test.txt"; print `ps -orss $$`; $a=$_ for @a; print `ps -orss $$`' RSS 7612 RSS 55024 real 0m1.001s user 0m0.908s sys 0m0.088s
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Loading a part of the file to array using Tie::File
by ikegami (Patriarch) on Nov 24, 2017 at 21:52 UTC | |
by haukex (Archbishop) on Nov 25, 2017 at 16:18 UTC | |
by ikegami (Patriarch) on Nov 26, 2017 at 07:51 UTC | |
by haukex (Archbishop) on Nov 26, 2017 at 09:04 UTC | |
Re^6: Loading a part of the file to array using Tie::File
by karlgoethebier (Abbot) on Nov 25, 2017 at 19:42 UTC | |
Re^6: Loading a part of the file to array using Tie::File
by Anonymous Monk on Nov 25, 2017 at 05:49 UTC |