Soldato
- Joined
- 24 Nov 2002
- Posts
- 16,378
- Location
- 38.744281°N 104.846806°W
Further to a previous thread, an interesting challenge 
What's the quickest way to read a random line from a 1Gb text file (static files, so can cache number of lines). Obviously I don't want to load the entire file into memory. I've got 15s in Perl:
My ruby version was 30s+
.
Something I could call within Ruby would be nice, e.g. Ruby itself, bash, perl, python, java etc. I'll be running this across 45 files, millions of times, so speed is essential.

What's the quickest way to read a random line from a 1Gb text file (static files, so can cache number of lines). Obviously I don't want to load the entire file into memory. I've got 15s in Perl:
Code:
perl -e 'srand; rand($.) < 1 && ($n = $_) while <>; print $n' FILE
My ruby version was 30s+

Something I could call within Ruby would be nice, e.g. Ruby itself, bash, perl, python, java etc. I'll be running this across 45 files, millions of times, so speed is essential.
Last edited: