akash19jain's blog

By akash19jain, history, 10 months ago, In English,

I submitted a solution in C and was pretty sure that it would pass the given Time Limit, but I received a TLE and was surprised. Next I submitted the exact same solution in C++ and it passes in 0.6 Sec while the one in C couldn’t pass in even 5 seconds. Can anyone tell me as to why this happened? Link to C solution: https://codeforces.com/contest/282/submission/62757397 Link to C++ solution: https://codeforces.com/contest/282/submission/62757469

  • Vote: I like it
  • -3
  • Vote: I do not like it

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

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

10 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Short answer: #define __USE_MINGW_ANSI_STDIO 0.

Long answer: The MinGW wrapper of the printing functions has a performance bug, see https://stackoverflow.com/questions/13970675/massive-fprintf-speed-difference-without-std-c99 for more details.

Adding #define __USE_MINGW_ANSI_STDIO 1 to the C++ version will make it similarly slow.

Warning: in that case cast long double to double before passing to printf. See https://codeforces.com/blog/entry/46517#comment-309889 and https://stackoverflow.com/questions/4089174/printf-and-long-double for more details.

Alternatively it's possible to change printf to putchar. In this case it's easy to do because there's only one character.

Test code:

// #define __USE_MINGW_ANSI_STDIO 1
int main(){
    for(int i=0;i<100000;++i)

With __USE_MINGW_ANSI_STDIO = 1, printf takes 300ms, putchar takes 0ms. With __USE_MINGW_ANSI_STDIO = 0, printf takes 15ms, putchar takes 0ms.