macco macco - 3 months ago 12
C++ Question

c++ performance improvement for a simple function

I have a function in my program that gets called many many times. It is not particularly slow, it is just that it gets called so many times that any little improvement can help a lot.

inline static void addFanVertex(GLfloat x,GLfloat y, GLfloat r, GLfloat g, GLfloat b) {
tf.push_back(x);
tf.push_back(y);
tf.push_back(r);
tf.push_back(g);
tf.push_back(b);
tfe.push_back((tf.size()/5)-1);
}


tf and tfe are vectors:

std::vector<GLfloat> Renderer::tf;
std::vector<GLuint> Renderer::tfe;


I use std::reserve already to set their correct capacity. (Doesn't have significant performance improvements)

Anyone know something that would speed it up slightly?

Answer

I think there is no so many thing to do about your function. A little bit context could help in this case.

For example it could be no necessary push values in the vector in each cycle, or maybe the operator[] could be used.

Anyway just seeing that, I'd say to try something like:

  1. Check in disassemble whether the arguments are passed by stack allocation or by registers (it depend on the compiler). In case of stack you could "compress" your arguments in a struct and pass just that argument to the function. Moreover that struct should be pre-allocated with a static or a member function in the caller.

  2. You could think to use noexcept specifier (in case your compiler support it). That may enable some optimized compiler's operations. This in accordance with the fact the resources are pre-allocated correctly in the program and you trust that code.

  3. Use an profiler to see which micro-operations (also assembly lines) are the bottleneck of the function.

Comments