RoadTo2k's blog

By RoadTo2k, history, 6 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  

»
6 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).

»
6 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).

»
6 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 \
    )
»
6 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?

  • »
    »
    6 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.

    • »
      »
      »
      6 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.

      • »
        »
        »
        »
        6 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

        • »
          »
          »
          »
          »
          6 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.