Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which operation is more expensive?(a <= b and a != 0)

Reply
Thread Tools

Which operation is more expensive?(a <= b and a != 0)

 
 
Phil Carmody
Guest
Posts: n/a
 
      03-11-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Joe keane) writes:
> In article <(E-Mail Removed)>,
> Phil Carmody <(E-Mail Removed)> wrote:
> >So you don't like unsigned types for counters then?

>
> It's typical C to use 'int' unless something else is needed.


Sloppiness is typical too.

Possibly without exception, when the standard library provides an
interface for a function that implies operating over an array of
objects, the number of objects is specified as an unsigned type.
The sanest internal behaviour for those functions is to have the
same unsigned type as the index controlling the loop. So there
are plenty of very common examples of at least implicit unsigned
counters.

> It's probably an error if the counter comes in negative [or negative if
> it were signed if it's unsigned]


can't parse that, sorry

>, but maybe running zero times is fine.
>
> beg = 8;
> end = 6;
> for (j = beg; j < end; j++)
> ...


Ewwwww, integer overflow. Were you attemting to put forward a case
*for* the use of unsigned integers, as at least you'd know that you'd
wrap.

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      03-12-2012
On Mar 12, 12:08*am, Phil Carmody <(E-Mail Removed)>
wrote:
> (E-Mail Removed) (Joe keane) writes:
> >, but maybe running zero times is fine.

>
> > * * beg = 8;
> > * * end = 6;
> > * * for (j = beg; j < end; j++)
> > * *...

>
> Ewwwww, integer overflow. Were you attemting to put forward a case
> *for* the use of unsigned integers, as at least you'd know that you'd
> wrap.


Maybe I'm being daft, but when does this overflow?
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      03-12-2012
On 03/12/2012 03:05 AM, Harald van Dijk wrote:
> On Mar 12, 12:08�am, Phil Carmody <(E-Mail Removed)>
> wrote:
>> (E-Mail Removed) (Joe keane) writes:
>>> , but maybe running zero times is fine.

>>
>>> � � beg = 8;
>>> � � end = 6;
>>> � � for (j = beg; j < end; j++)
>>> � �...

>>
>> Ewwwww, integer overflow. Were you attemting to put forward a case
>> *for* the use of unsigned integers, as at least you'd know that you'd
>> wrap.

>
> Maybe I'm being daft, but when does this overflow?


When the j++ expression is executed with j==INT_MAX, which will happen
unless something is done inside the loop to prevent it from happening.
Were you perhaps thinking of beg=6, end=8?
--
James Kuyper
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      03-12-2012
On Mar 12, 12:59*pm, James Kuyper <(E-Mail Removed)> wrote:
> On 03/12/2012 03:05 AM, Harald van Dijk wrote:
>
> > On Mar 12, 12:08 am, Phil Carmody <(E-Mail Removed)>
> > wrote:
> >> (E-Mail Removed) (Joe keane) writes:
> >>> , but maybe running zero times is fine.

>
> >>> beg = 8;
> >>> end = 6;
> >>> for (j = beg; j < end; j++)
> >>> ...

>
> >> Ewwwww, integer overflow. Were you attemting to put forward a case
> >> *for* the use of unsigned integers, as at least you'd know that you'd
> >> wrap.

>
> > Maybe I'm being daft, but when does this overflow?

>
> When the j++ expression is executed with j==INT_MAX, which will happen
> unless something is done inside the loop to prevent it from happening.
> Were you perhaps thinking of beg=6, end=8?


It says "j < end" not "j != end"
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-12-2012
On 03/12/2012 09:24 AM, gwowen wrote:
> On Mar 12, 12:59 pm, James Kuyper <(E-Mail Removed)> wrote:
>> On 03/12/2012 03:05 AM, Harald van Dijk wrote:
>>
>>> On Mar 12, 12:08 am, Phil Carmody <(E-Mail Removed)>
>>> wrote:
>>>> (E-Mail Removed) (Joe keane) writes:
>>>>> , but maybe running zero times is fine.

>>
>>>>> beg = 8;
>>>>> end = 6;
>>>>> for (j = beg; j < end; j++)
>>>>> ...

>>
>>>> Ewwwww, integer overflow. Were you attemting to put forward a case
>>>> *for* the use of unsigned integers, as at least you'd know that you'd
>>>> wrap.

>>
>>> Maybe I'm being daft, but when does this overflow?

>>
>> When the j++ expression is executed with j==INT_MAX, which will happen
>> unless something is done inside the loop to prevent it from happening.
>> Were you perhaps thinking of beg=6, end=8?

>
> It says "j < end" not "j != end"


No, my misreading of that code was more complicated than that - but it's
not worth going into. Harald was right, and Phil and I were both wrong -
the code shown does not result in overflow.

The code shown is probably defective; as written either beg and end are
reversed, or the loop was meant to go downward. However, if beg=8 and
end=6 are simplifications for more complicated statements that,
depending upon the values of the other variables, sometimes result in
beg < end, the more complicated statements could be part of a reasonable
program.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2012
James Kuyper <(E-Mail Removed)> writes:
[...]
>>>>> (E-Mail Removed) (Joe keane) writes:
>>>>>> , but maybe running zero times is fine.
>>>
>>>>>> beg = 8;
>>>>>> end = 6;
>>>>>> for (j = beg; j < end; j++)
>>>>>> ...

[...]
> The code shown is probably defective; as written either beg and end are
> reversed, or the loop was meant to go downward. However, if beg=8 and
> end=6 are simplifications for more complicated statements that,
> depending upon the values of the other variables, sometimes result in
> beg < end, the more complicated statements could be part of a reasonable
> program.


I think the code was intended to demonstrate a loop that executes 0
times, as implied by the preceding "but maybe running zero times is
fine".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2012
Phil Carmody <(E-Mail Removed)> writes:
[...]
> Possibly without exception, when the standard library provides an
> interface for a function that implies operating over an array of
> objects, the number of objects is specified as an unsigned type.

[...]

And that unsigned type is almost always size_t.

The only exception I can find is the "%n" printf format, which expects a
pointer to an int (or you use "%ln" for long, or "%lln" for long long).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      03-13-2012
James Kuyper <(E-Mail Removed)> writes:

> On 03/12/2012 09:24 AM, gwowen wrote:
> > On Mar 12, 12:59 pm, James Kuyper <(E-Mail Removed)> wrote:
> >> On 03/12/2012 03:05 AM, Harald van Dijk wrote:
> >>
> >>> On Mar 12, 12:08 am, Phil Carmody <(E-Mail Removed)>
> >>> wrote:
> >>>> (E-Mail Removed) (Joe keane) writes:
> >>>>> , but maybe running zero times is fine.
> >>
> >>>>> beg = 8;
> >>>>> end = 6;
> >>>>> for (j = beg; j < end; j++)
> >>>>> ...
> >>
> >>>> Ewwwww, integer overflow. Were you attemting to put forward a case
> >>>> *for* the use of unsigned integers, as at least you'd know that you'd
> >>>> wrap.
> >>
> >>> Maybe I'm being daft, but when does this overflow?
> >>
> >> When the j++ expression is executed with j==INT_MAX, which will happen
> >> unless something is done inside the loop to prevent it from happening.
> >> Were you perhaps thinking of beg=6, end=8?

> >
> > It says "j < end" not "j != end"

>
> No, my misreading of that code was more complicated than that - but it's
> not worth going into. Harald was right, and Phil and I were both wrong -
> the code shown does not result in overflow.


Me more than you - I started it!


--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      03-14-2012
On Mon, 27 Feb 2012 18:16:59 -0800 (PST), Peter Nilsson
<(E-Mail Removed)> wrote:

>Barry Schwarz <(E-Mail Removed)> wrote:
>> Ping Cheu <(E-Mail Removed)> wrote:
>> > I saw bellow optimization rule.
>> > * * * * for (i = 1; i <= n; i++)

>...
>> > * * * * for (i = n; i != 0; i--)

>...
>> In the general case, tha author is merely displaying is
>> lack of experience and imagination.

>
>I suppose BWK was demonstrating the same lack of experience and
>imagination when he wrote Exercise 2.9 in K&R2, implying...
>
> for (b = 0; x; x &= x - 1)
> b++;
>
>...was always faster than...
>
> for (b = 0; x != 0; x >>= 1)
> if (x & 01)
> b++;
>
>These sorts of tricks are still relevant, just considerably
>less significant.


Since BWK started the discussion with the caveat "In a two's
complement system ...", he specifically avoided making a general
assertion. Furthermore, he only claimed it was useful for writing a
faster bit counting function, not that it had some property of
universal superiority.

It wouldn't surprise me if the trick had other applications also. It
also wouldn't surprise me if there were other non-obvious tricks which
could be proven to increase efficiency.

But neither of those points has anything to do with my comment that
the author of the original assertion that started this thread was
making global pronouncements without any supporting basis.

--
Remove del for email
 
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
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Which operation is more expensive?(a <= b and a != 0) zaemin C Programming 5 06-04-2005 07:15 PM



Advertisments