I am looking for a controllable way (easy to set the time for delay) to slow down my C++ solution on online judges. (Mainly for UVa, g++ 4.8.2 -lm -lcrypt -O2 -std=c++11 -pipe
)
I've tried the following code:
{auto start=std::chrono::high_resolution_clock::now();
while (std::chrono::duration<double,std::milli>
(std::chrono::high_resolution_clock::now()-start).count()<2000);
}
But the solution was slowed down for about 1.6 seconds, not the expected 2 seconds, I don't know why.
I also tried std::this_thread::sleep_for
and usleep()
fom <unistd.h>
, but these almost didn't influence the runtime on online judges.
For std::this_thread::sleep_for
, I tried:
std::this_thread::sleep_for(std::chrono::milliseconds(2600));
The reason why I want to do this is my teacher often assign problems on these online judges, and our homework grader will submit our solutions to those online judges to check if they can get AC (Accepted). As a result, my solution will be counted twice in the ranking system and I think this is unfair for later users, especially when my solution was ranked at the top of the ranklist. So I prefer to slow down my solution to reduce the influence on other users before submitting it to the homework grading system.