Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subroutine perl with inline

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!

like image 383
Jaya Surya S Athikesavan Avatar asked Jun 19 '18 06:06

Jaya Surya S Athikesavan


People also ask

What is the function of the __ inline?

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.

How do I define a subroutine in Perl?

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.

How can you call a subroutine and identify a subroutine in Perl?

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.


1 Answers

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. (See constant.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.

like image 143
zdim Avatar answered Nov 15 '22 11:11

zdim