skip to Main Content

I’ve been coding with plain C for the last few months because of our university curriculum. The programs I write are quite small and very not-resource-demanding. As a result, compiling with gcc has always been an almost instantaneous process (40ms on my "biggest" program so far).

Recently though, I enrolled in a local competition, for which C++ is apparently the norm, and so I started practicing. Despite my programs still being extremely small (not even half a kB), I noticed that compiling with g++ has consistently been almost like a 0.2s pause, followed by the instantaneous compilation I was used to with gcc.

Here’s the code I’m currently testing it on:

//#include <bits/stdc++.h> // I commented this out thinking that maybe the problem was due to too many header files being included
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std; // I know this is kind of a bad practice but I've been told it's recommended in competitions for ease of writing

int mex(vector<int> arr, vector<int> subarr) {
    int min = INT_MAX;

    for (auto i : arr) {
        // If i doesn't exist in subarr
        if (find(subarr.begin(), subarr.end(), i) == subarr.end()) {
            if (i < min) {
                min = i;
            }
        }
    }

    return min;
}

int main () {
    vector<int> a = {0,1,2,3,4,5};
    vector<int> suba = {0, 1,3,4};

    cout << mex(a, suba) << endl;

    return 0;
}

This compiles in 240ms.
Here’s g++’s ftime-report:

Time variable                                   usr           sys          wall           GGC
 phase setup                        :   0.00 (  0%)   0.00 (  0%)   0.00 (  0%)  1562k (  2%)
 phase parsing                      :   0.16 ( 84%)   0.13 ( 87%)   0.29 ( 85%)    60M ( 83%)
 phase lang. deferred               :   0.01 (  5%)   0.01 (  7%)   0.02 (  6%)  5818k (  8%)
 phase opt and generate             :   0.02 ( 11%)   0.01 (  7%)   0.03 (  9%)  5468k (  7%)
 |name lookup                       :   0.05 ( 26%)   0.02 ( 13%)   0.03 (  9%)  2550k (  3%)
 |overload resolution               :   0.01 (  5%)   0.00 (  0%)   0.02 (  6%)  6891k (  9%)
 callgraph construction             :   0.01 (  5%)   0.00 (  0%)   0.01 (  3%)  1448k (  2%)
 df scan insns                      :   0.01 (  5%)   0.00 (  0%)   0.00 (  0%)  4128  (  0%)
 preprocessing                      :   0.01 (  5%)   0.05 ( 33%)   0.05 ( 15%)  1805k (  2%)
 parser (global)                    :   0.03 ( 16%)   0.02 ( 13%)   0.07 ( 21%)    16M ( 23%)
 parser struct body                 :   0.00 (  0%)   0.03 ( 20%)   0.03 (  9%)    11M ( 15%)
 parser function body               :   0.03 ( 16%)   0.00 (  0%)   0.03 (  9%)  3231k (  4%)
 parser inl. func. body             :   0.01 (  5%)   0.02 ( 13%)   0.01 (  3%)  1474k (  2%)
 parser inl. meth. body             :   0.01 (  5%)   0.00 (  0%)   0.03 (  9%)  5790k (  8%)
 template instantiation             :   0.07 ( 37%)   0.02 ( 13%)   0.08 ( 24%)    19M ( 27%)
 constant expression evaluation     :   0.00 (  0%)   0.00 (  0%)   0.01 (  3%)    85k (  0%)
 expand                             :   0.00 (  0%)   0.01 (  7%)   0.00 (  0%)   367k (  0%)
 integrated RA                      :   0.00 (  0%)   0.00 (  0%)   0.00 (  0%)  2092k (  3%)
 LRA non-specific                   :   0.00 (  0%)   0.00 (  0%)   0.01 (  3%)    14k (  0%)
 symout                             :   0.01 (  5%)   0.00 (  0%)   0.00 (  0%)  6907k (  9%)
 rest of compilation                :   0.00 (  0%)   0.00 (  0%)   0.01 (  3%)   212k (  0%)
 TOTAL                              :   0.19          0.15          0.34           73M

I’m using gcc and g++ versions 11.4.0 on Ubuntu 22.04.

Is this a normal occurrence? Is g++ normally this noticeably slower than gcc? Or is there an underlying issue on my computer?

2

Answers


  1. Yes, this is normal . The C++ language is far more complicated than the C language, and there’s more going on in the standard headers.

    Enterprise-level projects can take hours to build, so be very glad yours is only taking 0.2 seconds !

    Login or Signup to reply.
  2. Yes it is pretty common from my experience as C++ has many features and more complicated syntax.

    If your code doesn’t use any C++ features the compile time may be almost identical, but using C++ introduces many more checks and logic that adds to the complexity of the process.

    You can find a more detailed explaination for the reason here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search