8GB memory is not enough

  • Windows 7, 64 bit takes 1GB
  • Firefox takes 1GB
  • Virtualbox takes 1GB
  • and Lightroom takes 5.2GB

but growing up on C64, I will hate myself while I put that 16GB into my X230.

Advertisements

Surprising benchmark on Ruby’s STDIN.gets versus #each + #gets

I have accidentally run into some benchmark data on CodeChef with STDIN.each which, with large enough input data showed a 2x speedup against a simple #each + #gets.

#1:

n, k = STDIN.gets.chomp.split(' ').map {|x| x.to_i}
count = 0
(1..n).each do |i|
    x = gets.chomp.to_i
    count +=1 if x % k == 0
end
puts count

#2:

n, k = STDIN.gets.chomp.split(' ').map {|x| x.to_i}
count = 0
STDIN.each do |x|
    count +=1 if x.to_i % k == 0
end
puts count

The difference is minor:

(1..n).each do |i|
    x = gets.chomp.to_i

vs.

STDIN.each do |x|

If the dataset is 10^7 lines (each is an Integer, not larger than 10^9) then:

  • #1 runs in 44 seconds,
  • #2 runs in 20 seconds.

This might be a big issue for those who write code for such sites as: HackerRank, CodeChef, Interviewstreet or Codility. While the former two offers a place to practice coding, so this error can be addressed, the latter two are used for hiring candidates where Time Limit is often a factor of success and it is not possible to correct an entry once it has been submitted.

I need to go deeper on this issue, a wild guess would be that the #each loop that encapsulates the #gets could be opening-closing file handles, and that presents a serious overhead (multiplied 10^7 times).