Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In game programming, what are the specific C++ or STL features that causes performance hogs? [closed]

Tags:

c++

c

stl

My question is mostly about STL than the rest of the C++ that can be compared (I guess) to be as much fast as C a long as classes aren't used at every corner.

STL is standard for games and in engines like OGRE3D, but I was wondering that if STL's features are nice to use, the problem is while I don't really know how they work, I should know first what features can cause serious hogs before using them.

I'm very excited to begin that game programming school, and apparently there is no way I am going to not use those advanced features.

like image 620
gokoon Avatar asked Jun 28 '10 16:06

gokoon


3 Answers

Using STL tends to generate as good if not more efficient code than hand written code for many cases.

Use a profiler to see where you have problems.

Even where C++ STL might perform worse its code is likely to be less error prone. So only write code if the profiler shows there is an issue

like image 200
mmmmmm Avatar answered Nov 08 '22 01:11

mmmmmm


1) Debug builds. Severaly slow down many stl containers due to excessive error checking. At least on Microsoft compilers.
2) Excessive dynamic memory allocation. If you have routine that contains std::vector within it AND if you'll call it few thousand times per frame, it will be very slow and bottleneck will be somewhere within operator new or another memory allocation routine. If you'll turn this vector into some kind of static buffer (so you won't have to recreate it every time), it will be much faster. Memory allocation is slow. If you have a buffer, it is normally better to reuse it instead of making a new one during each call.
3) Using inefficient algorithms. For example, using linear search instead of binary search, using wrong sort algorithms (for example, quick sort, heap sort are faster than bubble sort on unsorted data but insertion sort can be faster than quicksort on partially sorted data). Searching instead of using std::map, and so on.
4) Using wrong kind of container. For example, std::vector isn't suitable for inserting elements at random places. std::deque, while is comparable with std::vector (random access), allows fast push_front and push_back, can be 10 times slower than std::vector if you subtract two iterators (on MSVC, again).

like image 28
SigTerm Avatar answered Nov 08 '22 00:11

SigTerm


Unless you're building the entire engine from scratch, you're not going to notice a difference in using or not using C++ classes or STL. In general, most of the time the CPU is going to be running code that's not even written by you anyway. Plus the overhead imposed by any scripting languages you implement (Lua, Python, etc) will eclipse any small performance penalty you may incur by using C++/STL.

In short, don't worry about it. Writing good OOP code is better than trying to write super-fast code from the get-go. "Premature optimization is the root of much programming evil."

like image 6
Timothy Baldridge Avatar answered Nov 08 '22 00:11

Timothy Baldridge