You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some functions, especially ones computing a simple operation, are less efficient to call, rather than copy paste the underlying code.
As an example:
function test(num a) -> num {
return (a * 4) + (a * 4) - (a / 2)
}
To call this function, we need 3+3 instructions, (copy a, set return destination, jump to function code, jump back to return destination, copy result), while the function result is computed in five instructions. If this function is used only once, it is not only faster, and more efficient for output length, to simply insert the content inline.
While modern compilers can detect which functions can benefit from being inline automatically, an easier solution is to let the developer mark a function as inline with syntax, for example as such:
inline function test(num a) -> num {
return (a * 4) + (a * 4) - (a / 2)
}
Some languages also allow the user to mark a function as "constexpr", to tell the compiler that it can precalculate the result, if the input is known in advance.
These optimizations are especially beneficial where copying is expensive, so it will help if you ever decide to add structs or classes.
The text was updated successfully, but these errors were encountered:
Some functions, especially ones computing a simple operation, are less efficient to call, rather than copy paste the underlying code.
As an example:
To call this function, we need 3+3 instructions, (copy a, set return destination, jump to function code, jump back to return destination, copy result), while the function result is computed in five instructions. If this function is used only once, it is not only faster, and more efficient for output length, to simply insert the content inline.
While modern compilers can detect which functions can benefit from being inline automatically, an easier solution is to let the developer mark a function as inline with syntax, for example as such:
Some languages also allow the user to mark a function as "constexpr", to tell the compiler that it can precalculate the result, if the input is known in advance.
These optimizations are especially beneficial where copying is expensive, so it will help if you ever decide to add structs or classes.
The text was updated successfully, but these errors were encountered: