Here is an example code why I started thinking about this
void renderScene(void)
{
//Clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
// Question : Declaring posAttrib as a static variable is good? or bad?
GLint posAttrib = glGetAttribLocation(programID, "vtxPosition");
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, ((void*)(0)));
glEnableVertexAttribArray(posAttrib);
glDrawArrays(GL_POINTS, 0, 3);
//Double buffer
glutSwapBuffers();
}
This is about OpenGl, and renderScene()
will be called whenever my screen renders.
So I think if I declare posAttrib
as a static variable, it will reduce unnecessary calls.
But I learned using static variable causes many problems so dont use it. I considered what would be best. So I searched about it and read those posts
Declaring variables inside loops, good practice or bad practice?
Optimization for global and static variables
This post said but the initialization is likely to take place at each loop iteration.
and you probably should not worry about this kind of optimization.
And I've read many posts advising against using static variables.
But in other opinions,using static variables is recommended for actions that incur high memory copy costs.
I'm not sure when to use static variables based on my level of CS knowledge.
Should I measure the performance difference between using and not using static variables to make a decision? But I expect this method would be difficult to use as I collaborate with more people and the project grows larger. I want to have a basis for deciding whether or not to use it on my own when writing code that accounts for a small part of the entire project.
So my question is: What are the criteria to use a static variable for optimization?