开发者

Comparing Logical Conditions

There are two logical conditions :

A. ( Ls != Ec && Ls != Uc && Ls != Rfc )

B. (!(Ls==Ec || Ls == Uc || Ls == Rfc))

Variables are Ls, Ec, Uc, Rfc ( integers )

I found that both conditions are logically Same.

My Question/s is(are!) :

Is there any online tool/web to check multiple/two lo开发者_如何学运维gical conditions ? And perfomance wise which is better generally or with repect to C# .NET 3.5

Thanks.


Nothing online that I am aware of, but learning and applying De Morgan's Laws and Truth Tables will get you there yoruself.


C# compiler seems to generate an identical IL for both codes (both with debug and release build). Therefore, there must be no performance difference between two.

In fact, according to IL, C# compiler translates TestTwo to TestOne. When you see the compiled dll in Reflector, TestTwo became TestOne.

I compiled the following code and opened them in ILDASM.

    public bool TestOne(int l, int e, int u, int r)
    {
        return (l != e && l != u && l != r);
    }

    public bool TestTwo(int l, int e, int u, int r)
    {
        return (!(l == e || l == u || l == r));
    }

The following is what I saw in ILDASM (based on release build).

.method public hidebysig instance bool  TestOne(int32 l,
                                                int32 e,
                                                int32 u,
                                                int32 r) cil managed
{
  // Code size       19 (0x13)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  ldarg.2
  IL_0002:  beq.s      IL_0011
  IL_0004:  ldarg.1
  IL_0005:  ldarg.3
  IL_0006:  beq.s      IL_0011
  IL_0008:  ldarg.1
  IL_0009:  ldarg.s    r
  IL_000b:  ceq
  IL_000d:  ldc.i4.0
  IL_000e:  ceq
  IL_0010:  ret
  IL_0011:  ldc.i4.0
  IL_0012:  ret
} // end of method Program::TestOne

.method public hidebysig instance bool  TestTwo(int32 l,
                                                int32 e,
                                                int32 u,
                                                int32 r) cil managed
{
  // Code size       19 (0x13)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  ldarg.2
  IL_0002:  beq.s      IL_0011
  IL_0004:  ldarg.1
  IL_0005:  ldarg.3
  IL_0006:  beq.s      IL_0011
  IL_0008:  ldarg.1
  IL_0009:  ldarg.s    r
  IL_000b:  ceq
  IL_000d:  ldc.i4.0
  IL_000e:  ceq
  IL_0010:  ret
  IL_0011:  ldc.i4.0
  IL_0012:  ret
} // end of method Program::TestTwo

I used VS2008 sp1 (.NET 3.5 sp1) to test this code.


There is a saying "First make it work, then make it fast!". I would assume that the difference (if existing at all) between both version will not affect the overall performance of your application - at least in 99.99% of all applications. If your application is one of the remaining 0.001%, then you are writing very special and sophisticated high speed software. In that case you should not use theoretical tools. Measure it under real live conditions!


I believe it is dependent on the implementation...
On a .Net based platform, from what I remember, when using && operator, the expression gets evaluated Left to Right and if the left operand is a false, the right one is not even evaluated. Going with that logic, I feel Option A would be a chosen one and faster if one of those left most operands turn out to be false. I guess that could be the reason why IL generated prefers to go with TestOne method (By the way, great idea to infer what is the chosen path, thank you Chansik Im.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜