Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Optimizing a conditional

Reply
Thread Tools

Optimizing a conditional

 
 
Steve
Guest
Posts: n/a
 
      03-10-2009
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

--
pub 1024D/228761E7 2003-06-04 Steven Crook
Key fingerprint = 1CD9 95E1 E9CE 80D6 C885 B7EB B471 80D5 2287 61E7
uid Steven Crook <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      03-10-2009
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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-10-2009
Steve <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-10-2009
Steve wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      03-10-2009
On Tue, 10 Mar 2009 14:56:42 +0000 (UTC), Willem wrote in
Message-Id: <(E-Mail Removed)>:

> Yes it is. Perl does short-circuiting.

Thanks Willem,

Knowing it was referred to as short-circuiting made my Google efforts
considerably more productive.


--
pub 1024D/228761E7 2003-06-04 Steven Crook
Key fingerprint = 1CD9 95E1 E9CE 80D6 C885 B7EB B471 80D5 2287 61E7
uid Steven Crook <(E-Mail Removed)>
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      03-10-2009
On Tue, 10 Mar 2009 16:01:28 GMT, A. Sinan Unur wrote in
Message-Id: <Xns9BCA7A514E09asu1cornelledu@127.0.0.1>:

> 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

--
pub 1024D/228761E7 2003-06-04 Steven Crook
Key fingerprint = 1CD9 95E1 E9CE 80D6 C885 B7EB B471 80D5 2287 61E7
uid Steven Crook <(E-Mail Removed)>
 
Reply With Quote
 
Rasmus Villemoes
Guest
Posts: n/a
 
      03-10-2009
Steve <(E-Mail Removed)> writes:

> 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[s] 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);

--
Rasmus Villemoes
<http://rasmusvillemoes.dk/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Firefox optimizing websites (help) Tiernan Firefox 11 03-07-2005 12:14 AM
Optimizing ViewState Ben Fidge ASP .Net 4 02-18-2005 02:39 PM
Optimizing using the IncrementalBuild property ASP .Net 5 07-28-2004 05:49 PM
Optimizing perfomance on T3 line Christoph Schad Cisco 12 01-01-2004 08:40 PM



Advertisments