In the contest Ocean of Code, I encountered a timeout error. I tried timing my code and I discovered that one line lasted an especially long time.
There it is for Python3 :
x, y, my_life, opp_life, torpedo_cooldown, sonar_cooldown, silence_cooldown, mine_cooldown = [int(i) for i in input().split()]
This line takes around 30ms while the rest of the code takes around 1ms.
I’d like to think that it’s taking some time because the line is simply waiting for the input, but then it should be taken into account and it shouldn’t count in the mandatory response time (≤50ms). If it’s already taken into account then I have no idea why I have a timeout.
Another concern I have is the irregularity of the time taken by this one line (varies between 15 and 60ms). If it is in fact counted in the response time, then what should I do?
(I checked my code and I don’t have any while loop or recursive method that could cause the timeout)
You need to start your timer after you get the first input in a round. You are now measuring the time used in the Referee and the time used by your enemy.
Would you mind elaborating on that? I’m currently in a similarly strange position, where I lose most of my games due to timeouts, but I cannot see any reason for that in the code. It happens within the first turns and when I re-run in the IDE it seems to occur less often as opposed to the submitted code.
As for measuring past the input, my entire AI prints the output within 1ms. Measured between the end of reading the input (which is the original code from the template, no processing whatsoever) until the end of the game loop. In successful games I can sometimes see the timing to be as bad as 10ms, but never anywhere near 50ms.
Nevertheless, my last battles show 7 losses right when opening the view of which 5 are timeouts. This feels very flaky.
(I’m using Kotlin should that be relevant)
I finally found out what caused the problem just in case someone else runs into it as well:
After reading the initial input, I calculate the start position, print it, and then do some more precalculations. For some obscure reason the timing code seems to count the time used for the precalculations as part of my first actual turn time, hence, the timeout. Simply moving the
println for the start position to just before starting the game loop solved the timing issues and resulted in a ranking improvement of 20 positions in Bronze league.
Is this normal behavior or a bug for Ocean of Code specifically?
I finally found what caused my problem!
Instead of printing my output once per turn, I would print it in different parts :
print(“MOVE x y”, end = “”) then print("| TORPEDO x’ y’", end = “\n”)
resulting in a timeout error…
Correcting this made me go right to the next league!
Concerning your question about the bug you had, I have no idea…
It’s because the placement and the first movement are two separate turns in the referee, two calls to your bot. You get feedback on your initial placement in the first movement turn, so it has to be that way.
And by design the full 1s is for the first turn (placement phase) and every later turn has a reduced timelimit.
That’s unfortunate. In a language where the code is compiled Just In Time (e.g. Java), you want your main code to run during the first turn when you have plenty of time. You can warm up your code by calling it even though you don’t need it, but that will be harder for beginners.