Which operator is faster:?: Or &&

When developing ASP.NET applications, I often have to parse the boolean value specified in a string form, for example. from the query string, for example ?visible=true

I found two solutions for implementing 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; } } 

Which way do you think is preferable? And probably faster?

PS This is not a microopt, I just want to find out.

PPS I am not familiar with IL, so I decided to ask here.

+4
source share
6 answers

Do not 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 help;) And this implementation actually gives fewer op codes compared to the other two, and I believe that it will execute in fewer cycles, being faster than your attempts.

Thus, this is not only more readable imo, but also faster because it skips the if . The other two have the same op-codes, just switched the logic to check.

[Edit - compiled with Release flags - shorter IL]

If you look at the following three 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 produce 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.sb 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.sb 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.sb L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&) L_001b: pop L_001c: ldloc.0 L_001d: ret } 
+17
source

I think both of them are probably functionally wrong (maybe I donโ€™t understand what you are trying to do), but even if they are true, you donโ€™t care what is faster.

You really, really don't care.

+5
source

The first one is a bit nicer because it uses less code, so I would go with that.

I doubt that there is a big difference in speed between them, but if you really care, you should profile both of them - run it every million times or so and record the runtime.

+3
source

Actually, another way would be

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

Since b will be set to default(bool) (false) if TryParse fails.

And b MUST be set by TryParse because it is an out variable.

Do not micro-optimize, the code is so readable.

+3
source

The difference in speed between the two of them will be infinitesimal in comparison with the cost of the page life cycle. The main problem with them is that they are not very readable. Why don't you just do the following:

 return Request["visible"] == "true"; 

It reaches the same end, and it is absolutely clear. I do not see any value in what you do, it is just confusing.

+2
source

In this case, it will not have significant differences at all. Parsing a line takes a lot longer than a small difference between statements does not matter.

In the case when it matters, you should comment on the code to find out which is actually faster. Actual performance depends not only on one operator, but also on what operations are performed before and after this point in the code.

It is clear that the && operator is somewhat faster. Since there is no conditional jump, this is improved by parallel processing of sequential operations that modern processors do.

0
source

All Articles