Make C++ code compile faster, (taking more than hours to compile)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cmath>

using namespace std;

void Read_From_File(vector<double>&Time_Vector,vector<double>&Accel_X_Vector,vector<double>&Accel_Y_Vector)
{
    double Time=0,Accel_X=0,Accel_Y=0,X_Position=0, Y_Position=0;

    ifstream inFile("input3.txt");
    if(inFile.fail())
    {
        cout << "error reading from infile" << endl;
    }
    while(inFile>>Time>>Accel_X>>Accel_Y>>X_Position>>Y_Position)
    {
        Time_Vector.push_back(Time/1000);
        Accel_X_Vector.push_back(Accel_X);
        Accel_Y_Vector.push_back(Accel_Y);
    }
}

vector<double>Velocity_X_Vector_R_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
    vector<double>X_Velocity_R_Rsum;
    double Velocity_X_R_Rsum = 0;
    double Sum =0;

    X_Velocity_R_Rsum.push_back((Time_Vector[0]*Accel_X_Vector[0]));
    for (int i=1; i<Time_Vector.size()-1; i++)
    {
        Velocity_X_R_Rsum=(Velocity_X_R_Rsum+Accel_X_Vector[i]*(Delta_T));
        X_Velocity_R_Rsum.push_back(Velocity_X_R_Rsum);
    }
    Velocity_X_R_Rsum=(Velocity_X_R_Rsum+Accel_X_Vector[Time_Vector.size()-1]*(Time_Vector[Time_Vector.size()-1]-Time_Vector[Time_Vector.size()-2]));
    X_Velocity_R_Rsum.push_back(Velocity_X_R_Rsum );

    return X_Velocity_R_Rsum;
}

vector<double>Velocity_X_Vector_L_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
    vector<double>X_Velocity_L_Rsum;
    double Velocity_X_L_Rsum = 0;

    X_Velocity_L_Rsum.push_back((Time_Vector[0]*Accel_X_Vector[0]));

    for (int i=0; i<Time_Vector.size(); i++)
    {
        Velocity_X_L_Rsum=(Velocity_X_L_Rsum+(Accel_X_Vector[i]*(Delta_T)));
        X_Velocity_L_Rsum.push_back(Velocity_X_L_Rsum);
    }

    return X_Velocity_L_Rsum;
}

vector<double>Trapzoidal_X(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_X_Vector)
{
    vector<double>X_Velocity_T_Rsum;
    vector<double>X_Position_T_Rsum;
    double T_Rsum_X_Velocity=0;
    double T_Rsum_X_Position=0;

    T_Rsum_X_Velocity = 0.5*((Velocity_X_Vector_L_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[0]+
                              Velocity_X_Vector_R_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[0]));
    X_Velocity_T_Rsum.push_back(T_Rsum_X_Velocity);

    for (int i =0; i<Accel_X_Vector.size(); i++)
    {
        T_Rsum_X_Velocity = 0.5*((Velocity_X_Vector_L_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[i]+
                                  Velocity_X_Vector_R_Rsum( Delta_T,Time_Vector,Accel_X_Vector)[i]));

        X_Velocity_T_Rsum.push_back(T_Rsum_X_Velocity);

        T_Rsum_X_Position=(T_Rsum_X_Position+((X_Velocity_T_Rsum[i]+X_Velocity_T_Rsum[i+1]))*0.5*Delta_T);
        X_Position_T_Rsum.push_back(T_Rsum_X_Position);
    }
    return X_Position_T_Rsum;
}

vector<double>Velocity_Y_Vector_R_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
    vector<double>Y_Velocity_R_Rsum;
    double Velocity_Y_R_Rsum = 0;

    Y_Velocity_R_Rsum.push_back(Time_Vector[0]*Accel_Y_Vector[0]);
    for (int i=1; i<Time_Vector.size()-1; i++)
    {
        Velocity_Y_R_Rsum=(Velocity_Y_R_Rsum+(Accel_Y_Vector[i]*(Delta_T)));
        Y_Velocity_R_Rsum.push_back(Velocity_Y_R_Rsum);
    }
    Velocity_Y_R_Rsum = Velocity_Y_R_Rsum + Accel_Y_Vector[Time_Vector.size()-1]*(Time_Vector[Time_Vector.size()-1]-Time_Vector[Time_Vector.size()-2]);
    Y_Velocity_R_Rsum.push_back(Velocity_Y_R_Rsum);
    return Y_Velocity_R_Rsum;
}

vector<double>Velocity_Y_Vector_L_Rsum(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
    vector<double>Y_Velocity_L_Rsum;
    double Velocity_Y_L_Rsum = 0;

    Y_Velocity_L_Rsum.push_back(Time_Vector[0]*Accel_Y_Vector[0]);
    for (int i=0; i<Time_Vector.size(); i++)
    {

        Velocity_Y_L_Rsum= Velocity_Y_L_Rsum+Accel_Y_Vector[i]*(Delta_T);
        Y_Velocity_L_Rsum.push_back(Velocity_Y_L_Rsum);
    }

    return Y_Velocity_L_Rsum;
}

vector<double>Trapzoidal_Y(const double Delta_T,vector<double>Time_Vector,vector<double>Accel_Y_Vector)
{
    vector<double>Y_Velocity_T_Rsum;
    vector<double>Y_Position_T_Rsum;
    double T_Rsum_Y_Velocity=0;
    double T_Rsum_Y_Position=0;

    T_Rsum_Y_Velocity = 0.5*((Velocity_Y_Vector_L_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[0]+
                              Velocity_Y_Vector_R_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[0]));
    Y_Velocity_T_Rsum.push_back(T_Rsum_Y_Velocity);

    for (int i =0; i<Accel_Y_Vector.size(); i++)
    {
        T_Rsum_Y_Velocity = 0.5*((Velocity_Y_Vector_L_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[i]+
                                  Velocity_Y_Vector_R_Rsum( Delta_T,Time_Vector,Accel_Y_Vector)[i]));

        Y_Velocity_T_Rsum.push_back(T_Rsum_Y_Velocity);

    }
    for (int i=0; i<Y_Velocity_T_Rsum.size()-1; i++)
    {
        T_Rsum_Y_Position=(T_Rsum_Y_Position+((Y_Velocity_T_Rsum[i]+Y_Velocity_T_Rsum[i+1]))*0.5*Delta_T);
        Y_Position_T_Rsum.push_back(T_Rsum_Y_Position);
    }
    return Y_Position_T_Rsum;
}

int main()
{
    vector<double>Time_Vector;
    vector<double>Accel_X_Vector;
    vector<double>Accel_Y_Vector;

    Read_From_File(Time_Vector,Accel_X_Vector,Accel_Y_Vector);
    const double Delta_T =(Time_Vector[Time_Vector.size()-1]-(Time_Vector[0])) / (Time_Vector.size()-1);

    for ( int i=0; i<Time_Vector.size(); i++)
    {
//            cout<<Velocity_X_Vector_L_Rsum(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(15)
//            <<Velocity_Y_Vector_L_Rsum(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<setw(15)
//            <<Velocity_X_Vector_R_Rsum(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(15)
//            <<Velocity_Y_Vector_R_Rsum(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<endl;
        cout<<scientific;
        cout<<Time_Vector[i]<<setw(20)<<Trapzoidal_X(Delta_T,Time_Vector,Accel_X_Vector)[i]<<setw(20)<<
               Trapzoidal_Y(Delta_T,Time_Vector,Accel_Y_Vector)[i]<<endl;
    }
}

===============================================================================
My desired input is on this wapsite please Help

http://www1.datafilehost.com/d/686b8052

===============================================================================
THanks in advance
||=== Build finished: 0 errors, 9 warnings (0 minutes, 3 seconds) ===|


What compiler are you using? Maybe your computer is extremely slow?
codeblocks 12 and Devcpp

::DId u run it using the input i provided??::
I didn't run it because you only asked about compile time; which I believe is the time it takes to convert C++ code into assembly (OPcode, ASM, whatever you'd like to call it).

Ozwurld wrote:
codeblocks 12 and Devcpp


I believe those are not compilers, but IDEs (Integrated Development Environment). A compiler is the actual program that builds your program. Popular ones I believe are GNU GCC (g++), clang, and Microsoft's Visual Studio compiler (can't remember the name).

Now, you're asking me to run it; is it the running that takes hours to complete, or the compiling process? I think we need to get our terminology straight here.
Yeah sorry , its the running that is taking time *_* im a newbie at this
I've only had a quick glance at your code so far, but what I spotted was:

- passing vectors by value. I can see some vector<>& parameters, but also a lot of vector<>. Vectors should hardly ever be passed by value, as it triggers copying. in params should be passed as const ector<>&

- heavy use of vector<>::push_back(). This can be a problem if the vectors are big enough to trigger multiple reallocations while populating the vector. I possible, you should use vector<>::reserve() to pre-allocate the required storage in one go.

Andy

PS The Microsoft compiler is usually called the (Microsoft) Visual C++ compiler.
Thanx i will read up on vector<>::reserve() , and see how it works
reserve() isn't really the biggest problem here.

I get:
15,871 seconds runtime (estimated) as written
6,140 seconds runtime (estimated) passing vectors by reference to const (returning by value of course)
5,369 seconds runtime (est.) after every vector is reserve()'d
6.5 seconds runtime after rewriting Trapzoidal_Y and Trapzoidal_X to only call Velocity_Y_Vector_L_Rsum() and Velocity_Y_Vector_R_Rsum() once (instead of once every iteration)
0.04 seconds runtime after rewriting main() to only call Trapzoidal_Y and Trapzoidal_X once

Last edited on
BTW - when you say your code is taking over an hour, I presume you were talking about the optimized (or release) build?

Andy

Yes @ Andy, @ cubbi any suggestion as to how i can make it finish the build faster??
Yes @ Andy, @ cubbi any suggestion as to how i can make it finish the build faster??

I thought you said the problem was the time it takes to run, not the time it takes to build?

And cubbi already explained in his post what he did to make it run faster.
Topic archived. No new replies allowed.