Faster to create new variable or reuse existing one for math calculation?

Hello,

So I have a question that sprung out of curiosity as I was programming; is it better to reuse an existing variable or create a new variable for the following situation:

1
2
3
4
5
6
7
8
9
/* Is this faster: */

glm::mat4 newViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix);

/* Or is this faster? */

glm::mat4 existingViewModelMatrix; /* declared in class header - initial value assigned in class constructor */

existingViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix);


Just wondering if it makes a difference or not.

Thank you.
Hi,

the first is equivalent to the copy constructor.
glm:mat4 newViewModelMatrix ( (*existingViewMatrix) * (*existingModelMatrix) );

So it depends which one is cheaper the assignment or the copy constructor. This varies from class to class and therefore cannot be answered in general.

Regards,
mathes
Last edited on
Topic archived. No new replies allowed.