CONSTANT = 1000000000000000000
array = (1..CONSTANT).to_a
start = Time.now
array.each do |i|
if 1000 < i < CONSTANT * 9 / 10
elsif i > CONSTANT * 9 / 10
else
end
end
finish = Time.now
puts "Running time: #{finish - start} seconds"
I wrote the above script in an attempt to figure out how much time saving could be achieved by re-ordering the control branches. The script froze my machine immediately after being run, which couldn’t be terminated via CTL + C.
Could someone please point out what happened there?
You are creating a huge array which you don’t even need. That is forcing your machine to start paging (it would take up 5% of the 64-bit address space and your machine certainly doesn’t have that much RAM). Your code is not making it to the loop (the condition in the if statement isn’t valid ruby).