Program is geting very slow because of cout

Hi all,

I am writing a program, about a thousand lines, and I put some cout in the code. But the code is running very slow, so I noted that the problem is the cout inside the loops, they repeat many many times.

Then I changed the cout to output to file but then it was clear that something is going in a not desired way, the hdd led become flashing all the time the program is running.

So, I need a way to do log of the execution of the program.
Last edited on
i dont think is 'cuz of your code... unless you have infinite loops or memory leaks coused by undeleted pointers.... if you have a slower computer, try find a way to make your code shorter
HD accesses are probably not going to be much faster than cout (they might be a little faster, but HD writes are still very slow). So doing a giant text dump to a file will greatly slow down your program any way you slice it.

How slow is "slow"? Several seconds/minutes/forever? How big is your log file getting? Several megabytes? Is your program completing or are you having to force it to shutdown before it exits normally?

Depending on your answer to the above, it might be that your program is spinning in an infinite loop, rather than it being bogged down by cout/file writes.

But anyway this is a shot in the dark. Without more info/code I can't really tell you what the problem is or how to solve it.


edit: blah I'm too slow -- CManowar replied before me ^^. Good catch on the undeleted pointers -- a big memory leak might also be the culprit! Nice.
Last edited on
I think this is the slow part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class vectors {
public:
    double module, phase;
    int x, y; // x for col, y for line: Position occupied in matrix

    int compare(vectors vector_a, vectors vector_b, std::ofstream &file_log) {
        // retorna 1 se os vetores forem iguais(parecidos)
        int match = 0;
        file_log << "vector_a: " << vector_a.module << " " << vector_a.phase << "\t";
        file_log << vector_a.x << "," << vector_a.y << endl;
        file_log << "vector_b: " << vector_b.module << " " << vector_b.phase << "\t";
        file_log << vector_b.x << "," << vector_b.y;
        //        cout << fabs(vector_a.module - vector_b.module) << endl;
        //        cout << fabs(vector_a.phase - vector_b.phase) << endl;
        if (fabs(vector_a.module - vector_b.module) < tau) {
            if (vector_a.phase == -vector_b.phase) {
                match = 1;
            }
        }
        return match;
    }
};


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
vector<vector<vector<double> > >checkAsymmetricVectors(// Find/remove assimetric vectors
        vector<vector<vector<double> > >matrix_mod_phase, std::ofstream &file_log
        ) {
    file_log << "\tChecking for Asymmetric vectors." << endl;
    vector<vector<vector<double> > >matrix_asymmetric;
    matrix_asymmetric = matrix_mod_phase;
    //    matrix_asymmetric[0][i][j] moudules
    //    matrix_asymmetric[1][i][j] phases

    vectors vector_a, vector_b;

    int i_b, j_b;
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < cols; j++) {

            if (j < cols - 1) { // coordenadas do vetor_b
                i_b = i;
                j_b = j + 1;
            } else {
                i_b = i + 1;
                j_b = 0;
            }

            vector_a.module = matrix_mod_phase[0][i][j]; // load a
            vector_a.phase = matrix_mod_phase[1][i][j];
            vector_a.x = i;
            vector_a.y = j;
            //            if (matrix_asymmetric[0][i][j] != 0) {
            while (i_b < lines) {
                while (j_b < cols) {

                    if (matrix_asymmetric[0][i][j] != 0) {
                        // Check if this vector was not yet removed

                        vector_b.module = matrix_mod_phase[0][i_b][j_b]; // load b
                        vector_b.phase = matrix_mod_phase[1][i_b][j_b];
                        vector_b.x = i_b;
                        vector_b.y = j_b;

                        //                        cout << vector_a.module << "\t" << vector_a.phase << "\t|\t"; // show both vectors
                        //                        cout << vector_b.module << "\t" << vector_b.phase << endl;

                        if (vector_a.compare(vector_a, vector_b, file_log)) {
                            asym_vectors = asym_vectors + 1;
                            file_log << " Asymmetric vectors!\n" << endl;
                            matrix_asymmetric[0][i][j] = 0;
                            matrix_asymmetric[1][i][j] = 0;
                            matrix_asymmetric[0][i_b][j_b] = 0;
                            matrix_asymmetric[1][i_b][j_b] = 0;
                        } else {
                            file_log << " NOT\n" << endl;
                        }

                    }
                    j_b++;

                }
                i_b++; // go next line
                j_b = 0; // go col zero
            }
            //            }//

        }
    }

    return matrix_asymmetric;
}
How slow is "slow"? Several seconds/minutes/forever? How big is your log file getting? Several megabytes? Is your program completing or are you having to force it to shutdown before it exits normally?


Also have you tried removing the file logging? Does that speed it up or is it still slow even without it?
The program is completing ok. With the cout it take about 20 min and without the cout it takes only 20 seconds - is a lot faster.
How much time it takes with the outputing to the file?
So,

when I post this question the objective was to confirm that the problem are the cout or file out.

And...now I think there are no way to undergo this problem: I need to remove the not very required logs.

I think that is it.

Thanks for all!
Hey CManowar, using output to file or using cout, the time it takes is more or less the same. About 20mins using output to file and about 20 seconds without output to file. This times are not exacly but I am sure that it runs ten times faster without the cout or fileout.

Thanks for your attention.
I realize that the couts were definitely the culprit, but...

You should also consider passing parameters by const reference, as you are definitely taking a performance hit every time you call checkAsymmetricVectors since the vector<vector<vector<double>>> is getting passed by value (ie, completely copied) onto the stack to call the function, and then you are copying it a second time on line 6 of the function.

Likewise, your compare function takes copies of vectors which results in about 20 bytes of stack usage per vectors instance.

In my experience, a very output-heavy program can be dramatically sped up by redirecting output to a file. "Dramatically" being something like 40 times. It obviously depends on the speed of the device the file is being written to and how much output the program produces.
A terminal emulator is one of the slowest I/O devices, though, so you're almost guaranteed to see a performance upgrade.
Last edited on
Topic archived. No new replies allowed.