Optimizing a conditional

S

Steve

Hi all,

I'm wondering what is the best way to performance optimize a conditional
in Perl.

In my particular instance I have:

if (simple_condition and complex_condition) {
do_foo();
};

simple_condition is just a Boolean.
complex_condition is a large regex.

Is the above syntax optimal, or would I be better doing:
if (simple_condition) {
if (complex_condition) {
do_foo();
};
};

There's also the variation of:
if (simple_condition) {
do foo() if complex_condition;
};

I don't see how the above is different from the previous example but
thought I'd mention it just in case. :)

Steve
 
W

Willem

Steve wrote:
) Hi all,
)
) I'm wondering what is the best way to performance optimize a conditional
) in Perl.
)
) In my particular instance I have:
)
) if (simple_condition and complex_condition) {
) do_foo();
) };
)
) simple_condition is just a Boolean.
) complex_condition is a large regex.
)
) Is the above syntax optimal, <snip>

Yes it is. Perl does short-circuiting.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
A

A. Sinan Unur

Hi all,

I'm wondering what is the best way to performance optimize a
conditional in Perl.

In my particular instance I have:

if (simple_condition and complex_condition) {
do_foo();
};

simple_condition is just a Boolean.
complex_condition is a large regex.

Is the above syntax optimal,

Here is a silly example to show you how you would benchmark something
like this:

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw( cmpthese );

cmpthese( -5, {
first_false => sub {
if ( 0 and exp(log(1)) ) {
my $x = 'a';
}
},
first_true => sub {
if ( 1 and exp(log(1)) ) {
my $x = 'a';
}
},
}
);

__END__

C:\DOCUME~1\asu1\LOCALS~1\Temp\t> t
Rate first_true first_false
first_true 6499122/s -- -89%
first_false 59826552/s 821% --


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
G

Gunnar Hjalmarsson

Steve said:
I'm wondering what is the best way to performance optimize a conditional
in Perl.

In my particular instance I have:

if (simple_condition and complex_condition) {
do_foo();
};

simple_condition is just a Boolean.
complex_condition is a large regex.

Is the above syntax optimal, or would I be better doing:
if (simple_condition) {
if (complex_condition) {
do_foo();
};
};

The difference is most likely negligible. Please see

http://perldoc.perl.org/perlop.html#Logical-And
There's also the variation of:
if (simple_condition) {
do foo() if complex_condition;
};

I don't see how the above is different from the previous example

Me neither.
 
S

Steve

Here is a silly example to show you how you would benchmark something
like this:

Very helpful, thanks

Rather than thanking every reply individually, please let me extend my
gratitude for all the helpful answers I've received.

Steve
 
R

Rasmus Villemoes

Steve said:
Hi all,

I'm wondering what is the best way to performance optimize a conditional
in Perl.

In my particular instance I have:

if (simple_condition and complex_condition) {
do_foo(); ....
};
There's also the variation of:
if (simple_condition) {
do foo() if complex_condition;
};

"Programming Perl", in the subsection Time Efficiency, suggests "Use
statement modifiers... instead of full-blown conditionals. [This]
avoid the overhead of entering and leaving a block". So combining
the short-circuiting optimization with this advice gives

do_foo() if (simple_condition and complex_condition);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top