C ++ code is executing slow

I spent the last week porting the Branch & Cut recursive algorithm from Matlab to C ++ and hoped to see a significant reduction in solution time, but as incredible as it may sound, it was the other way around. Now I am not a C ++ expert, so I downloaded a sleepy profiler and tried to find potential bottlenecks. I would like to ask if I am presenting the correct conclusions from this or if I am looking in a completely wrong direction.

I skipped the code in 137 seconds and this is what the profiler shows (many other entries below, but they don't matter):

enter image description here

So, if I get it right, 98 seconds were wasted creating new objects and 34 seconds wasted freeing memory (i.e. deleting objects).

I'll go over my code and see where I can do something better, but I also wanted to ask if you have any hints of common mistakes or bad habits that cause this behavior. The only thing that comes to my mind is that I am using a lot of temporary std :: vectors in my code to compute stuff, so this can be slow.

Lest you be blind, I won't post my code before I go over it a bit, but if I can't solve this on my own, I'll be back.

+3


source to share


4 answers


Yes, it std::vector

can be costly if misused. The biggest hit to performance can be reallocation - since the size must be dynamically adjustable and you have constraints that must be in contiguous memory, reallocations happen when you add new items outside of the already allocated ones.

This is why you must either declare the size in advance. If you know you will have to hold on to elements n

, then declare it as

 std::vector<MyClass> x(n);

      

or

 std::vector<MyClass> x;
 x.reserve(n);

      



instead

 std::vector<MyClass> x;

      

then n

push_backs.

If it is still slow after that, you can provide std::vector

using a dedicated distributor. Hope you don't get to this.

+5


source


Everything in Matlab is an object reference. So when you pass them in, you're doing the equivalent of copying a pointer (roughly an int in size depending on several factors), as opposed to copying the entire matrix, which is probably larger.

Without seeing any code, I cannot say for sure, but I suspect that you are copying a lot of objects, not copying references to them. I suggest you take a look at smart pointers like std::shared_ptr

.



You didn't make it clear, but you should compile with optimizations. ( g++ -O3

.) Some expensive copies and other operations can be optimized, but not all.

Also, if you are new to C ++ you shouldn't use new

. This is for experts and should only be used after discussion with a colleague and a strong cup of coffee. (Of course, new

can be used on your behalf by some containers, for example std::vector

.)

+1


source


As Lucian Grigore said, std :: vector is expensive. Not only that, any part of your code that constantly creates objects, moves data, reallocates memory, or even deletes it can take a long time. If what Alex Chamberlain says is true, there is a big bottleneck. If not, I would remember you (or tell you if you don’t know it) that almost anything you do to make your software cheaper in memory consumption will pay off in performance. Conversely, it is also true that more memory consumption means that the processor has fewer computational capabilities, because you are making cache memory of everything that was previously computed. This is basic, but sometimes we forget about it.

0


source


avoid calling new and delete critical code in time. It's slow. Or use a fast memory manager (like SmartHeap).

-1


source







All Articles