Optimizing a conditional

Discussion in 'Perl Misc' started by Steve, Mar 10, 2009.

  1. Steve

    Steve Guest

    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 <>
     
    Steve, Mar 10, 2009
    #1
    1. Advertising

  2. Steve

    Willem Guest

    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
     
    Willem, Mar 10, 2009
    #2
    1. Advertising

  3. Steve <> wrote in
    news::

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

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Mar 10, 2009
    #3
  4. 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
     
    Gunnar Hjalmarsson, Mar 10, 2009
    #4
  5. Steve

    Steve Guest

    On Tue, 10 Mar 2009 14:56:42 +0000 (UTC), Willem wrote in
    Message-Id: <>:

    > 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 <>
     
    Steve, Mar 10, 2009
    #5
  6. Steve

    Steve Guest

    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 <>
     
    Steve, Mar 10, 2009
    #6
  7. Steve <> 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 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/>
     
    Rasmus Villemoes, Mar 10, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    366
    Guest
    Jul 28, 2004
  2. Ben Fidge

    Optimizing ViewState

    Ben Fidge, Feb 17, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    452
    =?Utf-8?B?U2hhdW4=?=
    Feb 18, 2005
  3. Alejandro Penate-Diaz

    optimizing DropDownLists

    Alejandro Penate-Diaz, Apr 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Karl Seguin
    Apr 8, 2005
  4. =?Utf-8?B?RGlmZmlkZW50?=

    Question on optimizing a piece of code

    =?Utf-8?B?RGlmZmlkZW50?=, Jun 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    407
    Patrice
    Jun 9, 2005
  5. Alec S.
    Replies:
    10
    Views:
    10,271
    Alec S.
    Apr 16, 2005
Loading...

Share This Page