Efficient way to call a function and use the result to call another function

I am an amateur and study OpenGL for a personal project; I don't have a deep understanding of C ++ (now I'm fine), so I'm sorry if this is a dumb or obvious question.

I'm curious about the advantages and disadvantages of each of the ways to do the following (if any), assuming that this piece of code starts each tick of the game loop:

glm::vec3 camera_pos = g_Camera->GetPosition();

some_func(camera_pos.x, camera_pos.y, camera_pos.z);

against

some_func(g_Camera->GetPosition().x, g_Camera->GetPosition().y, g_Camera->GetPosition().z);

My guess is that the 1st version is better for performance, since this is only one function call, but I'm not sure if the compiler will optimize the second version so that it doesn't make a difference in general? I would prefer to use the second version for convenience and readability, but most of all I would like to do a more effective thing. I try not to do too much premature optimization, but that was something I had been thinking about for a long time.

+4
source share
2 answers

, , . , . , , getPosition , , , , .

, , . . .

, - ,

+3

.

some_func :

void some_func(const glm::vec3& pos) {...}

:

some_func(g_Camera->GetPosition());
0

Source: https://habr.com/ru/post/1670225/


All Articles