it’s my first post here.
I know that the question may seem vague i’ll try to be clear…
globally without targeting any interpreted language (well i’m using C# currently but i think the answer should work for others too…)
I wonder, does this type of call :
Afunction(var1,var2,AnotherFunction(var3,var2,AthirdFunction(Avector.z)),Mathf.RoundToInt(Avector.x));
Will run faster than :
Type var1 = avalue;
Type var2 = avalue;
Type var3 = avalue;
Type var4 = AthirdFunction(Avector.z);
Type var5 = Mathf.RoundToInt(Avector.x);
Type var6 = AnotherFunction(var3,var2,var4);
Afunction(var1,var2,var6,var5);
I know that the second way is easier to read, but does it runs faster, knowing that i’ll probably call this function extensively (lets say, in a graphic app, every frame).
And about memory use, is it better to create more variables to destroy it right after using the function, or to directly code everything inside “Afunction” while declaring as less variables as possible…
(and for understanding : Afunction(), AnotherFunction() and AthirdFunction() are already declared somewhere else)
I hope this type of question is not outside of the forum rules of use…
Assuming a reasonable compiler, the algorithms you use in Afunction, AnotherFunction, and AthirdFunction have way more impact on performance than anything else that you asked above. Generally you worry about what you’ve asked once you have the most efficient function possible.
Plus, compilers will inline function calls which means that you may call
But if you were to look at what passes for assembler output for your compiler, things may not look that way.
There is overhead with invoking a function call, and it is system and compiler architecture that dictates what that overhead will be.
If this were an interpreted language like awk or BASIC, then the second code block, calling things outside a function linearly, will run faster. Or more correctly it did back in 1980’s on DEC PDP’s.