Title: Message
Well, assume something like this:
 
if ($tformat =~ /^[ymd]{1,4}([^ymd]{1})[ymd]{1,4}[^ymd]{1}[ymd]{1,4}$/i or
    $tformat =~ /^([ymd]{1,4})[^ymd]{1}([ymd]{1,4})[^ymd]{1}([ymd]{1,4})$/i) {
}
 
Actually this is a weird example since I have accomplished with something different what I needed. However I believe it would be good for me to know for a future case if the short-circuit will avoid extra processing. As you can see, on the second part of the OR I have three capturing parentheses which create $1, $2, and $3 which are more expensive that just $1. The perldocs state that once used, (in this case $2 and $3) they have to be provided for every match. So if my program matches the left side OR it would not provide $2 and $3 to every other regex, thus saving resources. I want to know if this is true.
 
Regards,

Javier Moreno
==============
Softtek/GXS
EFS NearShore - TradeWeb 2nd Tier
Phone: (5281) 81-53-2082
Dialcomm: *879-2918

...when you have eliminated the impossible, whatever remains, however improbable, must be the truth.
- Sherlock Holmes

-----Original Message-----
From: Jean-Sebastien Guay [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 31, 2003 10:18 AM
To: Tim Vale; [EMAIL PROTECTED]
Subject: Fw: performance

Oops, meant to send this to the list...
 
Why doesn't the list put its own address in the reply-to field of all messages delivered? Most of the time, we'll want to reply to the list.
 
J-S
 
----- Original Message -----
Sent: Thursday, July 31, 2003 9:06 AM
Subject: Re: performance

Tim,
 
> A statement that results in a true or false with a defined directive has to be completed fully before the directive can be met.
 
Not true. As the original poster noted, the logical operators in almost any language are short-circuit (including Perl, C/C++, Java, etc). That means that if the _expression_ is sure to return true after only evaluating, for example, the left side of an OR operation, then the right side will never be evaluated. (because True OR <anything> = True and False AND <anything> = False)
 
Try this simple script :
 
____________________ start script ____________________
 
#!/usr/bin/perl -w
use strict;
 
sub isExecuted {
    print "I was executed!\n";
    return 1;
}
 
if (1 || isExecuted) {
    print "First condition True\n";
}
else {
    print "First condition False\n";
}
 
print "\n";
 
if (0 || isExecuted) {
    print "Second condition True\n";
}
else {
    print "Second condition False\n";
}
 
print "\n";
 
if (1 && isExecuted) {
    print "Third condition True\n";
}
else {
    print "Third condition False\n";
}
 
print "\n";
 
if (0 && isExecuted) {
    print "Fourth condition True\n";
}
else {
    print "Fourth condition False\n";
}
____________________ end script ____________________
 
The results should be:
 
____________________ start output ____________________
 
First condition True
 
I was executed!
Second condition True
 
I was executed!
Third condition True
 
Fourth condition False
____________________ end output ____________________
 
 
The implications of this is that if you have a boolean _expression_ that depends on a variable and a function call, the result of both being given to an AND or OR operator to give the final result, you should always put the variable first, so that Perl can short-circuit without ever calling the function. The other way around would mean that the function would always be called, no matter what the variable's value is.
 
But to return to the original poster's question, could you give us some example code? I'm not sure what you mean by "This IF captures one parentheses and the 'or' part captures three on regexes."... With an example we might be able to answer your question.
 
J-S
 
----- Original Message -----
From: Tim Vale
Sent: Wednesday, July 30, 2003 5:17 PM
Subject: RE: performance

I can't speak for the interpretation of Perl (as an interpretive langauge) in the processing of a command structure or statement (though the Benchmark module is a useful way to judge code performance). However, I can speak about the way "all" langauges end up, on any platform, as a set of memory locations that set or hold values dependant on flow results (what used to be called machine code and is now "sadly" missing from parlance - is that that its sad or I am ?).
 
A statement that results in a true or false with a defined directive has to be completed fully before the directive can be met. In other words if you stack a load of decisions in a pile you can never jump out of the pile with an answer until all parts of the stack has been processed. The deeper the stack - the deeper the process (or interupt) has to run to obtain an answer. Logic.
 
In the days heady of Basic interpretation (god I'm old) the stacks used to be very very deep and we only had 8 bits to play with in our stacks - these days the registries where these stacks reside are much wider (32bits etc) - so the interpreted code gets "streamlined" into single sweeps of process (or interupt). But, I am certain that my initial premis holds true - if you need to make a decision, and even if you get the answer straight away, you still have to pass through the stack before you can act on it. I doubt perl does it different as its based/built on C (I do doubt it is - but please let me know if you know different - "I would be impressed").
 
So in answer - and remember the overhead of the interpretation will usually be greater than any of the questions asked within it - the deeper you make a stack the more process and shuffling of memory locations is going to be needed - the longer it is going to take to answer the question.
 
All this is sub-bonnet (ie deep in the bowels of your CPU etc) - I would suggest you try different ways of scripting the questions - run them with Benchmark (or your stop watch of choice) and answer each specific point individually.
 
One thing I have learned here - there is a billion ways to do everything in Perl - the right way is your way.
 
T
 
 
 
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Moreno, Javier (GXS, Softtek)
Sent: 30 July 2003 17:52
To: Activeperl (E-mail)
Subject: performance

Hi all,

Would anyone know this? I am attempting a data-formatting function. I know the 'or' operator will short-circuit. So I have an IF that has an 'or' operator. This IF captures one parentheses and the 'or' part captures three on regexes. I know that the more captured parentheses the greater penalty on performance. So I am wondering if my program short-circuits with the or, will there be no penalty for those non-processed three capturing parentheses?

Regards,

Javier Moreno
==============
Softtek/GXS
EFS NearShore - TradeWeb 2nd Tier

...when you have eliminated the impossible, whatever remains, however improbable, must be the truth.
- Sherlock Holmes


Reply via email to