yhtyyar.s's blog

By yhtyyar.s, history, 6 years 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

| Write comment?
»
6 years 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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yes, it is better one. Thank you for sharing

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If someone is facing difficulty in configurations, check my working Code here.

    Happy Coding :)

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

»
5 years 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.

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

Maybe use tools as gprof instead of writing these macros?