[Chicago-talk] Which "if" structure is faster?
Steven Lembark
lembark at wrkhors.com
Mon Feb 23 10:39:14 CST 2004
-- Jay Strauss <me at heyjay.com>
> I don't really have that option unfortunately
Update a closure after the first pass through the
handler. First time around the variable contains
first time arguments, after that it contains the
remaining arguments:
my $sub = 0;
my $after =
sub
{
remaining_passes @remaining_args;
# returns nonzero on completion
$finished
};
my $prior =
sub
{
first_pass @first_time_args;
$sub = $after;
};
...
# no if logic required: the first pass through the
# closure calls do_soemthing and then updates the
# reference variable to call the after-the-fisrt-pass.
#
# these could be the same sub with different args or
# two different subs: the infinite loop exits whenever
# the $after closure returns true.
for(;;) { $sub->() && last }
Point here is that no if-logic is required: the first pass
updates the value $sub to accomodate the remianing passes.
You could have multiple state handlers this way, so long
as each one knows how to pass of the processing to the
state handler that follows it.
--
Steven Lembark 2930 W. Palmer
Workhorse Computing Chicago, IL 60647
+1 888 359 3508
More information about the Chicago-talk
mailing list