SandorGarcia's blog

By SandorGarcia, history, 3 years ago, In English,

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?

 
 
 
 
  • Vote: I like it
  • +23
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I resubmitted your code and it gives me opposite results,

http://codeforces.com/contest/734/submission/22361482 http://codeforces.com/contest/734/submission/22361478

By the way, you shouldn't use array for max if you want speed, use normal if else.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It is unbelievable how a same code can get such different measures. This should not be happening!

    22343033 vs 22361482

    Those interested use the "compare" button on the upper right corner so you can check it is in fact the exact same code

    Anyway problem solved. First code was faster, and by such a great margin, because Codeforces servers seem to have a 'error margin' of roughly one second for the same Ruby submission.

    Has anyone observed such a behavior with any other language?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I am guessing this is because Ruby's Garbage Collector is not very good and in some submissions it runs it before the program ends.