I have a Perl function, which does not return any value. It does not take any arguments also.
sub test {
#do my logic
}
Can I do as :
sub test() {
#do my logic
}
Will the subroutine test be inlined? Will this work? (meaning will the function call be replaced with the function definition. And will my program execute faster?)
The function test() is being called 5000 times. And my Perl program takes a longer time to execute than expected. So I want to make my program faster. Thanks in advance!
The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run.
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.
To call subroutines: NAME(LIST); # & is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/imported. &NAME(LIST); # Circumvent prototypes. &NAME; # Makes current @_ visible to called subroutine.
This is answered in Constant Functions in perlsub
Functions with a prototype of
()
are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without&
. Calls made using&
are never inlined. (Seeconstant.pm
for an easy way to declare most constants.)
So your sub test()
should be inlined if it satisfies the above conditions. Nobody can tell without seeing the function, so either show it or try.
This is most easily checked with B::Deparse, see further down the linked perlsub
section.
I would urge you to profile the program to ensure that the function call overhead is the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With