Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which operator is faster: ?: or &&

While developing ASP.NET applications I often need to parse a boolean value given in string form, e.g. from a query string like ?visible=true

I found two solutions to implement the parsing:

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) && b;
    }
}

or

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) ? b : false;
    }
}

How do you think which way is preferred? And probably faster?

P.S. It's not a micro-opt, I just want to get know

P.P.S. I'm not familiar with IL so decided to ask here

like image 911
abatishchev Avatar asked Nov 27 '22 10:11

abatishchev


2 Answers

Don't micro optimize, make it readable.

I think this is more readable:

bool visible;
Boolean.TryParse(this.Request["visible"], out visible);
return visible;

readable variable names usually helps ;) And this implementation actually yields fewer op codes compared to the other two, and I assume it will perform in fewer cycles, being faster than both your attempts.

So, it's not only more readable imo, but also faster since it skips the if statement. The other two have equal op codes, just switched the logic around on the checking.

[Edit - compiled with Release flags - shorter IL]

If you look at the three following implementations:

public bool Visible1
{
    get 
    {
        bool b;
        return Boolean.TryParse(HttpContext.Current.Request["visible"], out b) && b;
    }
}

public bool Visible2
{
    get
    {
        bool b;
        return Boolean.TryParse(HttpContext.Current.Request["visible"], out b) ? b : false;
    }
}

public bool Visible3
{
    get
    {
        bool b;
        Boolean.TryParse(HttpContext.Current.Request["visible"], out b);
        return b;
    }
}

will yield the following IL code:

.method public hidebysig specialname instance bool get_Visible1() cil managed
{
    .maxstack 2
    .locals init (
    [0] bool b)
    L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
    L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
    L_000a: ldstr "visible"
    L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
    L_0014: ldloca.s b
    L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
    L_001b: brfalse.s L_001f
    L_001d: ldloc.0 
    L_001e: ret 
    L_001f: ldc.i4.0 
    L_0020: ret 
}

.method public hidebysig specialname instance bool get_Visible2() cil managed
{
    .maxstack 2
    .locals init (
    [0] bool b)
    L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
    L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
    L_000a: ldstr "visible"
    L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
    L_0014: ldloca.s b
    L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
    L_001b: brtrue.s L_001f
    L_001d: ldc.i4.0 
    L_001e: ret 
    L_001f: ldloc.0 
    L_0020: ret 
}

.method public hidebysig specialname instance bool get_Visible3() cil managed
{
    .maxstack 2
    .locals init (
    [0] bool b)
    L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
    L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
    L_000a: ldstr "visible"
    L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
    L_0014: ldloca.s b
    L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
    L_001b: pop 
    L_001c: ldloc.0 
    L_001d: ret 
}
like image 193
Mikael Svenson Avatar answered Dec 05 '22 01:12

Mikael Svenson


I think both of them are probably functionally wrong (maybe I don't understand what you're tryinig to do), but even if they are correct, you don't care which is faster.

You really, really don't care.

like image 43
MarkR Avatar answered Dec 05 '22 00:12

MarkR