175iq's blog

By 175iq, history, 2 months ago, In English

I am using sublime text 3 with pre compiled headers as mentioned in this blog.

The build that I am using is as follows :

{
"cmd" : ["g++ -std=c++17 $file_name -o $file_base_name && timeout 4s ./$file_base_name <input.txt >stdout.txt 2>stderr.txt"], 
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}

If I use <bits/stdc++.h>, it takes 1.2s to compile and run every time I run any code( eg. Hello World code) using Ctrl+B.

When I use "bits/stdc++.h" (and expect the compilation time to become less) it actually takes 5-6 s to compile and run even the "Hello World" program for the first time using Ctrl+B. From next time, it takes 0.3 s only but the first time, it takes as long as 5-6s.

For every new program that I write, it happens like this. For eg. If I change the "Hello World" program to print 10 numbers from 1-10, it will again take 5-6s to compile and run. Then from the second time, it will take 0.3-0.4 s.

The directory structure is as follows :

CP/

  • bits/
    • stdc++.h
    • stdc++.h.gch
  • code.cpp (this is the file in which I write the code)

I have tried finding some information online but couldn't find anything useful. Can someone please help me out?

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

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

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

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

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

»
2 months ago, # |
  Vote: I like it +4 Vote: I do not like it

Idk why this is downvoted but it is quite interesting to observe this only on the first run. I have observed it too.

I guess in the first run compiler has to verify that the pre compiled header is indeed valid to use. Every subsequent runs benefit from one time check?

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

    Why does it not happen with other people who are using pre-compiled headers ?

    Do you know of any fix to this ?

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

    Same with me the first compilation takes too much time and the subsequent ones compiles instantly. Weird indeed :(

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

      What should be the fix to this ?

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

        I haven't found any fix as of yet :(

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

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

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

Try precompiling the original stdc++.h header from the g++ folder (if later you want to undo this step, simply delete stdc++.h.gch from the same folder), then change "bits/stdc++.h" to <bits/stdc++.h>

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

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

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

Did you precompile the headers with the same flags as you are trying to compile your program?

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

    Yes. I pre compiled the headers using the same flags. But it still takes so much time for compiling and running the program.

    What is so strange is that this happens only on the first time on compilation and after wards it takes the expected time of 0.3s to run the program?

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

I have the same issue, in my case it take around 50s. I have tried numerous attempts but it's the same every time. I use the same flags to compile. I even precompiled iostream in the same way but its of no use. I thought that it was just me who had this issue cuz of my old laptop.

Anyways I usually code in Python and it works well in my PC. But this issue with C++ needs to be solved and I will update if I find any solution.

Editv I use VSCode so it's not text editor specific.

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

    I also followed the blog exactly but the issue is still there. I wonder why does it happen only with a few ?

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

      Try adding the -H compiler flag to check if the compiler is using the precompiled headers during compilations. i.e :

      "cmd" : ["g++ -std=c++17 $file_name -H -o $file_base_name && timeout 4s ./$file_base_name <input.txt >stdout.txt 2>stderr.txt"]
      

      If you are using a precompiled "bits/stdc++.h", it would show a successful hit depicted by an ! :

      TERMINAL OUTPUT IF PRECOMPILED HEADER IS BEING USED

      Else it would show a miss depicted by X and show all the files it's compiling :

      TERMINAL OUTPUT IF PRECOMPILED HEADER IS NOT BEING USED
      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks this helped!! Now it's fixed.

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

        Hi. I added -H in the build command and now I can see that it is actually using precompiled header but the time taken for first compilation is still the same which is about 5-6 s.

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        How to add this in Sublime?

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

    After adding "-H", I noticed that even though it took a lot more time to compile in the first time but still the precompiled header was used. So there must be some problem in finding the path to that file.. So What I did was:

    I added "-H" in default list of flags which I used in compiling.

    Updated VSCode's default compiler path to where my g++.exe was located. In my case "C:\MinGW\bin".

    Tested it on a few files and it's workinv fine.

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

      Thanks. Earlier it used to take 9 seconds, sometimes even 20 seconds. Now its 3 second at max.

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

      What should I do in sublime text apart from adding -H in the build ?

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

      luctivid, it is working for you now but I think it will become like before after some time. It has happened with me too. Once I freshly compile stdc++.h, it works fast in the beginning and then goes back to its previous state after some time.

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

        Well, it has not happened since then. Also you can try these additional steps if it is still not resolved in your case.

        1. Check for an extra mingw folder in your working directory or root/C:. In case, you can temporarily delete it or move it somewhere else.

        2. use -v and -ftime-report flags during compilation and check for the possible issue.

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

          I am using linux(Ubuntu), so I am not using mingw. What will the -v and -ftime-report do ?

»
2 months ago, # |
  Vote: I like it -10 Vote: I do not like it

change compiler

»
2 months ago, # |
  Vote: I like it -24 Vote: I do not like it

How hard is it to just include some proper standard library headers? It only takes a few seconds to type it. I don't get why people are so dependent on #include <bits/stdc++.h>.

»
6 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

I had the same problem in windows. Although there was no problem in compilation time but the running time of freshly executable was around 7s — 10s and later executions in just 0.3s. I solved this issue by turning OFF my ANTI-VIRUS. Anti-virus check for viruses in newly created files (in our case *.exe's), which takes around 5s — 6s. After a short while, I switched to Ubuntu 20.04 and had never faced it again.

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

If you have a different directory for header files (or even precompiled header's), hope you are -I flag in your compilation command.

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

    Thanks for the reply.

    What does -l flag do?

    I am using Ubuntu 16.04 hence the problem can't be because of anti virus.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      If you add -I flag, it will tell compiler too looks for header files in this directory first, and then in its include directory(which is usually "/usr/include/x86-64-linux-gnu/c++/9/bits"). Let's assume you have a header file in the directory /folder1/folder2/folder/3/folder4/bits/stdc++.h. Then you should add "-I /folder1/folder2/folder/3/folder4/" in your compiling flags.