A doubt on Ruby code

Revision en1, by SandorGarcia, 2016-11-20 11:48:44

I just started learning Ruby and while solving D div 2 of the last round 379 I've found a behavior that puzzles me. Check out this two accepted solutions:

22342957 This one was the first I submitted

22343033 This one was an attempt to optimize (wanted to check how time changed since Ruby is slow, and how much of the time consumed went on IO)

As you can see the first submission was actually the fastest, and I have no clue why, since my intuition tells me that if anything the first one checks a condition twice and should therefore be slower

Also since the difference in time was around a second, it is significant enough as to determine whether a code gets accepted or not, so I wish to learn why does this happens.

And I know Ruby is inherently slow (relative to other languages) and it is the wrong tool for most Codeforces problems. In live contest i'll keep using good old c++. So please just focus on helping me in this particular problem and not on flaming the language.

In a more concise way, why is first code faster, and why is time difference so big?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English SandorGarcia 2016-11-20 11:48:44 1128 Initial revision (published)