From: Stefan.Lucks@uni-weimar.de
Subject: Re: Top 10 Worst C# Features
Date: Wed, 14 Oct 2015 20:17:59 +0200
Date: 2015-10-14T20:17:59+02:00 [thread overview]
Message-ID: <alpine.DEB.2.20.1510141943040.8228@debian> (raw)
In-Reply-To: <87si5d1jiw.fsf@jester.gateway.sonic.net>
[-- Attachment #1: Type: text/plain, Size: 2394 bytes --]
On Wed, 14 Oct 2015, Paul Rubin wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> Do you mean that there are reasons to permit !(a == b) and a != b to
>> have different values in some cases?
>
> I think he meant they should both return the same value, but it is ok if
> they don't use the same algorithm. Of course it may be possible for the
> compiler to optimize the first into the second. The idea is just that
> if (a != b) { ... } can be much faster than if (a == b) { ... } .
As I seem to understand, Ben questioned that either the test on equality
or on non-equality could be much faster than the other one.
In Ada syntax:
function "="(A, B: T) return Boolean is
begin
if Hash(A) /= Hash(B) then
return False
else return Slow_Equality_Test(A, B)
end "=";
function "/="(A, B: T) return Boolean is
begin
if Hash(A) /= Hash(B) then
return True
else return Not Slow_Equality_Test(A, B)
end "/="; -- if this where allowed in Ada
It seems quite clear that for any inputs X and Y, the expressions X = Y
and X /= Y should evaluate at the same speed. Or what am I missing?
Actually, "/=" could be faster than "=" if either where defined as
follows:
function "="(A, B: T) return Boolean
renames Slow_Equality_Test;
function "/="(A, B: T) return Boolean is
(Hash(A) /= Hash(B)); -- second case
Redefining "=" this way is just missing an optimization.
Redefining "/=" is that way is a bug.
So how could "/=" really be faster than "="?
My guess is that the devil's advocate, Maciej Sobczak, assumed that "=" is
already in use as identifier, instead of another identifier for the
Slow_Equality_Test, as I call it. Which means, we cannot employ the
optimization for equality. An independent "/=" would then allow us to
optimize non-equality.
But this would be a design flaw, and it might be easy to fix:
function "="(A, B: T) return Boolean is
-- optimized equality
begin
if Hash(A) /= Hash(B) then
return False
else return Outer_Package."="(A, B)
end "=";
-------- I love the taste of Cryptanalysis in the morning! --------
www.uni-weimar.de/de/medien/professuren/mediensicherheit/people/stefan-lucks
----Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universität Weimar, Germany----
next prev parent reply other threads:[~2015-10-14 18:17 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-02 10:59 Top 10 Worst C# Features Stefan.Lucks
2015-09-02 17:37 ` Álex R. Mosteo
2015-09-02 19:39 ` Randy Brukardt
2015-09-03 8:14 ` Georg Bauhaus
2015-09-03 9:26 ` Dmitry A. Kazakov
2015-09-03 11:39 ` G.B.
2015-09-03 12:00 ` G.B.
2015-09-03 13:59 ` Dmitry A. Kazakov
2015-09-03 19:12 ` Randy Brukardt
2015-09-04 7:33 ` Georg Bauhaus
2015-09-04 21:34 ` Randy Brukardt
2015-09-05 6:31 ` Dmitry A. Kazakov
2015-09-05 6:44 ` Georg Bauhaus
2015-09-05 7:07 ` Dmitry A. Kazakov
2015-09-05 6:45 ` Niklas Holsti
2015-09-05 7:21 ` Dmitry A. Kazakov
2015-09-05 12:07 ` Peter Chapin
2015-09-06 10:45 ` Georg Bauhaus
2015-10-13 19:57 ` Eryndlia Mavourneen
2015-09-05 7:16 ` Shark8
2015-09-03 13:47 ` Dmitry A. Kazakov
2015-09-03 8:51 ` gautier_niouzes
2015-10-01 14:03 ` Paul Colin de Gloucester
2015-10-14 8:00 ` Maciej Sobczak
2015-10-14 14:26 ` Ben Bacarisse
2015-10-14 16:50 ` Paul Rubin
2015-10-14 18:17 ` Stefan.Lucks [this message]
2015-10-14 19:54 ` Ben Bacarisse
2015-10-15 12:24 ` Maciej Sobczak
2015-10-15 13:59 ` Ben Bacarisse
2015-11-06 14:50 ` Nicholas Collin Paul de Gloucester
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox