Hi Codeforces. Today i saw GreenGrape solution of the problem D. Robot Vacuum Cleaner : 35053688. And i find there :
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
while (t--) {
clock_t z = clock();
solve();
debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
}
With this code he gets execution time of function solve()
. clock()
gives you current time. It is useful to avoid time limits. I have wrote helpful macro and decided to share with you :
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
with this u can get execution time of parts of your programm. Example:
int main () {
/*reading data*/
time__("dfs"){
/* dfs code */
}
/*some code*/
time__("solve"){
solve();
}
}
And programm will print something like dfs time : 0.2650s
, solve time:0.0010s
. You don't need to erase debugging part when you will submit it. Example submission: 35088800. Thank you for reading and I hope it will help you
A bit more respectable version
Yes, it is better one. Thank you for sharing
If someone is facing difficulty in configurations, check my working Code here.
Happy Coding :)
Thank you for sharing :)
But is it the judgement code which erases (or does something else) this debugging part of the submissions so we don't have to erase that part before submitting, or what?
Yes, you haven't to erase that part. There is example of submission at the end of blog.
I knew that and saw that examble, but I'm asking about why the code on our computers will led to printing the execution time wherese on the site will not.
It goes to the stderr stream, so it doesn't get looked at by judge
Thanks :D, now, from your comment, I can continue searching about this.
The 22 lines between lines 11 and 32 in the following ideone C++14 code is another alternative for writing the
time__
C++ preprocessor macro.ideone
The code uses the compiler directive
ONLINE_JUDGE
as a conditional compilation switch. When the online judge system passes it to the C++ compiler, the timer statements before and after the procedure call in the functiontime_monitor()
are not compiled. On the other hand, when the offline compilation does pass such directive, both statements are compiled, and the time monitoring code is executed at run-time.Maybe use tools as
gprof
instead of writing these macros?