[AccessD] CodePlex WCF Community code snippet

Gustav Brock Gustav at cactus.dk
Wed May 2 01:40:17 CDT 2012


Hi Shamil

Yes, that's strange. 
But, just curious, let me put the question why you pay so much attention to this tiny detail? If you evaluate all your code this way you would probably starve! I mean, even for a million iterations you most likely wouldn't be able to notice any difference in execution time.

/gustav


>>> mcp2004 at mail.ru 01-05-2012 20:26 >>>
Hi Gustav --

Yes, CompareObjects6

public static bool CompareObjects6<T>(T o1, T o2) where T : class
{
    // 'mere mortals' version refactored 2
    if (o1 != null && o2 != null) return o1.Equals(o2);
    return (o1 == o2);
}

beats even

public static bool CompareObjects8<T>(T o1, T o2) where T : class
{
    // Gustav's version refactored 2
    if (o1 == null || o2 == null) return (o1 == o2);
    return o1.Equals(o2);
}

for 'Optimize code' compilation version. It's funny that when you look at decompiled code you'll find that CompareObjects6  (decompiled) look like CompareObjects8 (original) and vice versa:

public static bool CompareObjects6<T>(T o1, T o2)
{
    if (o1 == null || o2 == null)
    {
        return o1 == o2;
    }
    else
    {
        return o1.Equals(o2);
    }
}


public static bool CompareObjects8<T>(T o1, T o2)
{
    if (o1 != null && o2 != null)
    {
        return o1.Equals(o2);
    }
    else
    {
        return o1 == o2;
    }
}

When 'Optimize code' option is not used then decompiled versions of CompareObjects6 and CompareObjects8 are:

public static bool CompareObjects6<T>(T o1, T o2)
{
    bool flag;
    bool flag1;
    if (o1 == null)
    {
        flag1 = true;
    }
    else
    {
        flag1 = o2 == null;
    }
    bool flag2 = flag1;
    if (flag2)
    {
        flag = o1 == o2;
    }
    else
    {
        flag = o1.Equals(o2);
    }
    return flag;
}

public static bool CompareObjects8<T>(T o1, T o2)
{
    bool flag;
    bool flag1;
    if (o1 == null)
    {
        flag1 = false;
    }
    else
    {
        flag1 = o2 != null;
    }
    bool flag2 = flag1;
    if (flag2)
    {
        flag = o1.Equals(o2);
    }
    else
    {
        flag = o1 == o2;
    }
    return flag;
}

Thank you.

-- Shamil




More information about the AccessD mailing list