Page 1 of 1

Performance Issue - Solution found;searching for Explanation

PostPosted: Sep 19, 2007 @ 9:18pm
by drgoldie
Today I ran into the most weird performance problem/bug I ever encountered:

My program conists of 3 main blocks of code. At the end of block 1 there is a function that does some math (pure integer, nothing special). For some reason that code turned out to be 10 times slower if I execute the other two main blocks of code afterwards. Without executing those other two block (after block 1 finished...) it runs at top performance.

Lots of debugging, benchmarking and testing reveiled that the problem was related to using an STL vector object (on the stack and only in that one function). I changed my code to use an old-school static array and the problem was gone. Yet, simply introducing the vector again into that function and just adding three integers brings the performance down again.

I solved by problem by not using a vector in that method, but I'd really be happy if somebody could explain what's going on here - and how this is connected to the other two blocks of code which have nothing to do with that vector...

Daniel

P.S.: All done in C++ using VS2005, SP1 and the Qt/Boost hotfix.


P.P.S.: If this was Java I'd say the garbage collector is malfunctioning, but in C++...?

Re: Performance Issue - Solution found;searching for Explana

PostPosted: Sep 19, 2007 @ 10:15pm
by hm

PostPosted: Sep 19, 2007 @ 10:16pm
by drgoldie

PostPosted: Sep 20, 2007 @ 5:24am
by hm

PostPosted: Sep 24, 2007 @ 3:04pm
by mm40
So in the function you are doing vector.push_back 3 times?

If so the reason it is slowing it down so much is because of memory allocation. I have seen this many times in my games, I have moved to use a custom vector class which gives me more control over memory allocation and deallocation, without that my games would run at 1fps. Basically I allocate/reserve all memory up front or in huge chunks. Allocating 1MB vs. 1byte takes almost the same time on the PPC. So when you do 3 push_backs that is allocating memory 3 times, which is very slow compared to some integer math.

What I do if I need memory on the fly is call vector.reserve(100) giving me space for 100 items, then the 3 calls to push_back only have the cost of 1 memory allocation, I don't even remember if you can do this std::vector as I've been using my custom vector for so long.

PostPosted: Sep 24, 2007 @ 3:12pm
by drgoldie
I understand what you are explaining, but I think it still should not react that intensively.

There is lots of dynamic memory handling in my code. Yet performance does not drop that much. This is not code that is called 10.000 times per frame. It is called only three times for a frame...

Daniel

PostPosted: Sep 24, 2007 @ 3:54pm
by mm40
What is the performance difference are you noticing, what are the timings?