Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Benefit of not defining the order of execution

Reply
Thread Tools

Benefit of not defining the order of execution

 
 
somenath
Guest
Posts: n/a
 
      02-11-2009
Hi All,

In case of relational and logical operator the order of execution is
not defined except for && and || which is left to right.
What could be the reason for not defining the order of execution?
What benefit it provides ? is it the speed?


Regards,
Somenath
 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-11-2009
On 11 Feb, 06:46, somenath <(E-Mail Removed)> wrote:

> In case of relational and logical operator the order of execution is
> not defined except for && and || which is left to right.
> What could be the reason for not defining the order of execution?
> What benefit it provides ? is it the speed?


it generally gives the compiler writer the freedom
to re-arrange operations. This can help with optimsation.

consider
printf ("%d %d", n m);

may be easier to implement if the arguments are evaluated
and pushed onto the stack if m is done before m.

Microsoft used to allow options so that a function
call was either "C style" or "Pascal style". Pascal
style was a bit quicker but couldn't handle varargs
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      02-11-2009

"somenath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
>
> In case of relational and logical operator the order of execution is
> not defined except for && and || which is left to right.
> What could be the reason for not defining the order of execution?
> What benefit it provides ? is it the speed?


I didn't know of such a rule but maybe code like the following was often
used:

char a[...]

if (i<sizeof(a) && a[i]...)...

This would break if the operands of && could be in any order.

--
Bartc

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-11-2009
Bartc wrote:
>
> "somenath" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi All,
>>
>> In case of relational and logical operator the order of execution is
>> not defined except for && and || which is left to right.
>> What could be the reason for not defining the order of execution?
>> What benefit it provides ? is it the speed?

>
> I didn't know of such a rule ...


There is no such rule. As he said, the order of execution is NOT defined
- the is no explicit statement to that effect. It is implicitly not
defined, by the absence of a specification of what the order should be.
Furthermore, it's clear that this lack of specification is not
accidental, because the rules for sequence points do impose a partial
order, just not a complete order. There's sequence points associated
with || and && (and also ?: and the comma operator).

> ... but maybe code like the following was often
> used:
>
> char a[...]
>
> if (i<sizeof(a) && a[i]...)...
>
> This would break if the operands of && could be in any order.


He wasn't asking why the order of execution is specified for && and ||.
He was asking whe the order of execution is NOT specified for the other
operators.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      02-11-2009
"Bartc" <(E-Mail Removed)> writes:

> "somenath" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi All,
>>
>> In case of relational and logical operator the order of execution is
>> not defined except for && and || which is left to right.
>> What could be the reason for not defining the order of execution?
>> What benefit it provides ? is it the speed?

>
> I didn't know of such a rule but maybe code like the following was
> often used:
>
> char a[...]
>
> if (i<sizeof(a) && a[i]...)...
>
> This would break if the operands of && could be in any order.


Exactly and why there is no such rule.

Possibly some lazy evaluation language might reorder the execution based
on hints of how expensive the evaluations are.
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-11-2009
On 11 Feb, 13:49, Richard <(E-Mail Removed)> wrote:
> "Bartc" <(E-Mail Removed)> writes:
> > "somenath" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...


> >> In case of relational and logical operator the order of execution is
> >> not defined except for && and || which is left to right.
> >> What could be the reason for not defining the order of execution?
> >> What benefit it provides ? is it the speed?

>
> > I didn't know of such a rule but maybe code like the following was
> > often used:

>
> > char a[...]

>
> > if (i<sizeof(a) && a[i]...)...

>
> > This would break if the operands of && could be in any order.


note a[i] isn't even evaluated if the first test fails.
So it's got to evaluate the first one first.

if ((i<sizeof(a) & a[i]...)...

is under no such constraint. It must (ok, since neither
appears to have a side effect the as-if rule applies)
evaluate both sides and can do so in any order.


> Exactly and why there is no such rule.
>
> Possibly some lazy evaluation language might reorder the execution based
> on hints of how expensive the evaluations are.


consider this

if (f() && g())

it must evaluate f and it might not evaluate g.
Some languages (eg Pascal) insist on evaluating
both f and g. The original example would break in Pascal


--
Nick Keighley

"Een schip op het strand is een baken in zee.
[A ship on the beach is a lighthouse to the sea.]"
- Dutch Proverb
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      02-11-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On 11 Feb, 13:49, Richard <(E-Mail Removed)> wrote:
>> "Bartc" <(E-Mail Removed)> writes:
>> > "somenath" <(E-Mail Removed)> wrote in message
>> >news:(E-Mail Removed)...

>
>> >> In case of relational and logical operator the order of execution is
>> >> not defined except for && and || which is left to right.
>> >> What could be the reason for not defining the order of execution?
>> >> What benefit it provides ? is it the speed?

>>
>> > I didn't know of such a rule but maybe code like the following was
>> > often used:

>>
>> > char a[...]

>>
>> > if (i<sizeof(a) && a[i]...)...

>>
>> > This would break if the operands of && could be in any order.

>
> note a[i] isn't even evaluated if the first test fails.
> So it's got to evaluate the first one first.
>
> if ((i<sizeof(a) & a[i]...)...
>
> is under no such constraint. It must (ok, since neither
> appears to have a side effect the as-if rule applies)
> evaluate both sides and can do so in any order.
>
>
>> Exactly and why there is no such rule.
>>
>> Possibly some lazy evaluation language might reorder the execution based
>> on hints of how expensive the evaluations are.

>
> consider this
>
> if (f() && g())
>
> it must evaluate f and it might not evaluate g.


Maybe it wasnt clear in the context - I was referring to the ability to
reorder.

> Some languages (eg Pascal) insist on evaluating
> both f and g. The original example would break in Pascal


I was referring to the potential reordering in other languages and one
reason might be hinted performance impact for thunks in something like
Haskell.

*might*.

Frankly I don't like the idea of reordering for any reason. The
programmer should have control of this powerful left to right ordering.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-12-2009
(E-Mail Removed) wrote:
>

.... snip ...
>
> Microsoft used to allow options so that a function call was
> either "C style" or "Pascal style". Pascal style was a bit
> quicker but couldn't handle varargs


It can if you add a rule that a final hidden parameter specifies
the count of arguments, or the bytes of arguments.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-12-2009
(E-Mail Removed) wrote:
>

.... snip ...
>
> consider this
>
> if (f() && g())
>
> it must evaluate f and it might not evaluate g.
> Some languages (eg Pascal) insist on evaluating
> both f and g. The original example would break in Pascal


Pascal doesn't have two forms of AND, so you just have to write it
differently. Try:

IF f() THEN
IF g() THEN BEGIN
(* whatever *) END;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      02-12-2009
James Kuyper <(E-Mail Removed)> writes:
> Bartc wrote:
>>
>> "somenath" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Hi All,
>>>
>>> In case of relational and logical operator the order of execution is
>>> not defined except for && and || which is left to right.
>>> What could be the reason for not defining the order of execution?
>>> What benefit it provides ? is it the speed?

>>
>> I didn't know of such a rule ...

>
> There is no such rule. As he said, the order of execution is NOT
> defined - the is no explicit statement to that effect. It is
> implicitly not defined


No, it's _explicitly_ "unspecified".

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
 
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
defining after execution frank C Programming 13 02-04-2010 06:36 PM
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
How to avoid the execution of a function at the time of defining. Moses Javascript 6 03-19-2007 09:34 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Re: Who stands to benefit from C# stream of MCAD? jd@aol.com MCSD 1 08-27-2003 12:40 PM



Advertisments