Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster way to call a method in c++

Tags:

c++

Quick question and I apologize if it sounds naive. What is faster in c++. A code like this:

ProgramsManager::CurrentProgram->Uniforms->Set(n1);
ProgramsManager::CurrentProgram->Uniforms->Set(n2);
ProgramsManager::CurrentProgram->Uniforms->Set(n3);
ProgramsManager::CurrentProgram->Uniforms->Set(...);

Or this one?

Uniforms* u = ProgramsManager::CurrentProgram->Uniforms;
u->Set(n1);
u->Set(n2);
u->Set(n3);
u->Set(...);

I know the second piece of code is faster in interpreted languages, but I feel like it makes no difference in compiled languages. Am I right? Thank you in advance

like image 834
Pierluigi Serra Avatar asked Oct 31 '15 16:10

Pierluigi Serra


1 Answers

The second might be faster, but it won't be faster by a lot.

The reason it might be faster is if the compiler cannot prove to itself that ProgramsManager::CurrentProgram->Uniforms could be changed by the calls to ...->Set. If it can't prove this, it will have to re-evaluate the expression ProgramsManager::CurrentProgram->Uniforms for each line.

However, modern CPUs are usually fairly quick at this kind of thing, and compilers are getting better.

like image 126
Chris Jefferson Avatar answered Sep 29 '22 09:09

Chris Jefferson