RoadTo2k's blog

By RoadTo2k, history, 8 months ago, In English,

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

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

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by RoadTo2k (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by RoadTo2k (previous revision, new revision, compare).

»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

A bit more respectable version

#define time__(d) \
    for ( \
        auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
        blockTime.second; \
        debug("%s: %lld ms\n", d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false \
    )
»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yes, you haven't to erase that part. There is example of submission at the end of blog.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        It goes to the stderr stream, so it doesn't get looked at by judge

        • »
          »
          »
          »
          »
          8 months ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          Thanks :D, now, from your comment, I can continue searching about this.

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

Thank you. This was what i needed.

»
3 weeks ago, # |
Rev. 29   Vote: I like it 0 Vote: I do not like it

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 function time_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.

»
3 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Maybe use tools as gprof instead of writing these macros?