Hi Artem,

Please reply to all recipients.

On Sun, 08 Mar 2015 17:11:52 +0300
Артём Варнайский <varnays...@mail.ru> wrote:

>  Hello, folks!
> I have such line:
> int rand(2)?1:-1
> It repeats in source code 5 times and executes over 800 times, due to
> performance issues, I don't want to wrap it with subroutine. How I can make
> this code more elegant?

Some ideas:

1. Implement the subroutine in XS using https://metacpan.org/release/Inline-C
or whatever.

2. Create a code like:

sub my_RAND_SUB_PLEASE_EXPAND
{
        return (int ( rand 2 ) ? 1 : -1);
}
.
.
.
        my $x = my_RAND_SUB_PLEASE_EXPAND();

.
.
.
        my $y = my_RAND_SUB_PLEASE_EXPAND();

And then compile the code to a more optimised version by passing it through a
filter such as:

perl -lapE 's/\Qmy_RAND_SUB_PLEASE_EXPAND()\E/(int ( rand 2 ) ? 1 : -1)/g'

That way both the preprocessed and postprocessed version will be valid (and
functional) Perl.

---

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Text Editors and IDEs - http://shlom.in/IDEs

Logic sucks. Morality sucks. Reality sucks. Deal with it!

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to