Ruby open file count lines
Copy link. Don't you think it will blow off heap, in case txt file is very large say 1 TB? Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. You signed in with another tab or window. Reload to refresh your session. Improve this answer. The ". Now it won't run on Windows.. I would take a small performance hit within the same complexity class to avoid portability issues.
If you are in a Unix environment, you can just let wc -l do the work. Jason WC is so fast that one probably won't need a progress counter. There's an edge condition: if the last line of the file doesn't have a newline, wc comes up one short.
This is by posix design, see backreference. I know, "Google harder! Show 1 more comment. I'm assuming the original poster was ok with reading through the file, just not having the entire contents of it in memory. JBoy JBoy 5, 12 12 gold badges 54 54 silver badges 92 92 bronze badges. Your system's wc has issues. I am quite sure you cannot do this with a huge file because its content will be entirely held in memory. Using readlines or read is not scalable. It's also slower than reading line-by-line using foreach once the files get beyond 1MB in size.
See stackoverflow. Exsemt Exsemt 1, 10 10 silver badges 21 21 bronze badges. I've rewritten a bit your benchmark , if you'd rather like I to edit your answer, just ping me here : — Ulysse BN. Yu Hao k 42 42 gold badges silver badges bronze badges. The following benchmark used a CSV file with 1,, lines of a data and 4 columns: user system total real 0. The scan forces in-memory and the file object is leaked.
You're comparing apples to oranges so, of course, there is going to be a big difference in speed. Not to mention a CSV entry could have newlines in a field without actually being a new csv row itself -- the File. Prev Next. Written by Gabor Szabo. If you have any comments or questions, feel free to post them on the source of this page in GitHub.
Source on GitHub. Comment on this post. Gabor can help your team improve the development speed and reduce the risk of bugs. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions.
0コメント