Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6860

Re: Slow -- VERY slow brain

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Slow -- VERY slow brain
Date 2011-06-17 19:59 +0200
Organization A noiseless patient Spider
Message-ID <itg42f$rmq$1@dont-email.me> (permalink)
References <itbdgg$aer$1@dont-email.me> <c799322d-93c3-48b3-b0c9-237c9af9c084@w10g2000yqh.googlegroups.com>

Show all headers | View raw


* Werner, on 17.06.2011 10:33:
> On Jun 16, 1:08 am, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> +use...@gmail.com>  wrote:
>> Well, I think, it's not just me.
>>
>> Since it's in the FAQ, and since we have discussed this for years, it must be
>> nearly all of us: we're idiots.
>>
>> I'm thinking of the argument for `unsigned` type, that you can do
>>
>>     void foo( unsigned n )
>>     {
>>         assert( n<  maxN );
>>         // blah
>>     }
>>
>> which is reportedly both more concise and more efficient than
>>
>>     void foo( int n )
>>     {
>>         assert( 0<= n&&  n<  maxN );
>>         // blah
>>     }
>>
>> But, the second one can always be written as
>>
>>     void foo( int n )
>>     {
>>         assert( unsigned( n )<  maxN );
>>         // blah
>>     }
>>
>> which is perhaps not quite as concise, but is exactly as efficient (doing
>> exactly the same), and moreover sort of forces you to think about what the first
>> function's code really does --  which is the same as this.
>
> I'm assuming maxN is of type unsigned.
>
> I'm not sure they do exactly the same thing. This all depends
> on the value of<n>  and the value of<maxN>, doesn't it?

Yes.


> #include<cstddef>
> #include<iostream>
>
> unsigned maxN = UINT_MAX - 5;
>
> void foo( int n )
> {
>    std::cout<<  "foo( "<<  n<<  " ):"<<  std::endl;
>
>    //Fails when (n == -7), uncomment to test next case
>    assert( 0<= n );
>
>    //Fails when (n == -6) only
>    assert( unsigned( n )<  maxN );
> }
>
>
> int main()
> {
>    int m = int( maxN ) - 1,
>         n = int( maxN );
>
>    //Fails because negative
>    foo( m );
>
>    //Fails because converted value exceeds maxN
>    // and because negative
>    foo( n );
> }
>
> In the case here above...
>
>    assert( 0<= n );
>
> ...clearly means something different, doesn't it?

Yes.


> Perhaps I'm a little [too] dull to see your point.

Yes. :-)

   ---

The efficiency argument is about testing x<maxN with unsigned operation in order 
to efficiently catch both the case of negative actual argument, and the case of 
x too large.

The efficiency argument therefore does not apply when a negative actual argument 
can be mapped to maxN via implicit conversion, so it requires maxN<unsigned(-1)/2.

My argument showed that it's bogus in its original formulation. Your argument 
shows that it's bogus when you allow larger maxN. In short, it's bogus.


Cheers & hth.,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Slow -- VERY slow brain "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-16 01:08 +0200
  Re: Slow -- VERY slow brain "Balog Pal" <pasa@lib.hu> - 2011-06-16 01:44 +0200
  Re: Slow -- VERY slow brain Puppet_Sock <puppet_sock@hotmail.com> - 2011-06-16 06:25 -0700
    Re: Slow -- VERY slow brain Noah Roberts <dont@email.me> - 2011-06-29 10:05 -0700
  Re: Slow -- VERY slow brain Keith H Duggar <duggar@alum.mit.edu> - 2011-06-16 07:00 -0700
  Re: Slow -- VERY slow brain "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-06-16 07:22 -0700
    Re: Slow -- VERY slow brain "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-06-16 07:31 -0700
      Re: Slow -- VERY slow brain "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-16 18:51 +0200
        Re: Slow -- VERY slow brain "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-06-16 13:10 -0700
          Re: Slow -- VERY slow brain "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-16 22:46 +0200
            Re: Slow -- VERY slow brain "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-06-17 07:38 -0700
              Re: Slow -- VERY slow brain Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-17 18:06 +0200
                Re: Slow -- VERY slow brain "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-06-17 10:49 -0700
        Re: Slow -- VERY slow brain "Paul" <pchristor@yahoo.co.uk> - 2011-06-17 18:27 +0100
  Re: Slow -- VERY slow brain Werner <werasm@gmail.com> - 2011-06-17 01:33 -0700
    Re: Slow -- VERY slow brain Öö Tiib <ootiib@hot.ee> - 2011-06-17 07:48 -0700
      Re: Slow -- VERY slow brain Werner <werasm@gmail.com> - 2011-06-17 09:39 -0700
    Re: Slow -- VERY slow brain "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-17 19:59 +0200

csiph-web