What if structure is faster? or not?>

D

Darren Clark

Is there a difference in performance betwen these 2 styles of code? As it both does the same thing...

if (row[JobseekerData.FLD_VISA_REQUIRED].ToString() == "1")
this.Visa.Text = "Yes";
else
this.Visa.Text = "No";

verses


this.Visa.Text = (row[JobseekerData.FLD_VISA_REQUIRED].ToString() == "1") ? "Yes" : "No";
 
E

EijiTek

From looking at the intermediate language for a similar example there is
very little difference between the two. Any difference between the two is
negligible in terms of performance.

I've included the IL from my test as an example so you can see what's going
on behind the scenes. You can see that the first type (if...else) branches
if the condition is false loads the string (True!), assigns it, then
branches out of the statement or (if the condition was false) loads the
string (False!), assigns it, and continues execution. The second type
(ternary operator) branches if the condition is true, loads the false string
then branches to the assignment otherwise if the condition was true it loads
the true string and assigns it. Long story short, we're talking about one
line IL more in the if...else version.

..method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 48 (0x30)
.maxstack 2
.locals init (bool V_0,
string V_1,
string V_2)
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: brfalse.s IL_000d
IL_0005: ldstr "True!"
IL_000a: stloc.1
IL_000b: br.s IL_0013
IL_000d: ldstr "False!"
IL_0012: stloc.1
IL_0013: ldloc.1
IL_0014: call void [mscorlib]System.Console::WriteLine(string)
IL_0019: ldloc.0
IL_001a: brtrue.s IL_0023
IL_001c: ldstr "False!"
IL_0021: br.s IL_0028
IL_0023: ldstr "True!"
IL_0028: stloc.2
IL_0029: ldloc.2
IL_002a: call void [mscorlib]System.Console::WriteLine(string)
IL_002f: ret
} // end of method TestApplication::Main
 
E

EijiTek

After you compile your assembly you can run ildasm <assembly name> from the
command prompt (you'll need to cd to the folder where your assembly is
stored).

Darren Clark said:
An extra question so i can do some more testing..

how do you view the IL?

EijiTek said:
From looking at the intermediate language for a similar example there is
very little difference between the two. Any difference between the two is
negligible in terms of performance.

I've included the IL from my test as an example so you can see what's going
on behind the scenes. You can see that the first type (if...else) branches
if the condition is false loads the string (True!), assigns it, then
branches out of the statement or (if the condition was false) loads the
string (False!), assigns it, and continues execution. The second type
(ternary operator) branches if the condition is true, loads the false string
then branches to the assignment otherwise if the condition was true it loads
the true string and assigns it. Long story short, we're talking about one
line IL more in the if...else version.

.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 48 (0x30)
.maxstack 2
.locals init (bool V_0,
string V_1,
string V_2)
IL_0000: ldc.i4.1
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: brfalse.s IL_000d
IL_0005: ldstr "True!"
IL_000a: stloc.1
IL_000b: br.s IL_0013
IL_000d: ldstr "False!"
IL_0012: stloc.1
IL_0013: ldloc.1
IL_0014: call void [mscorlib]System.Console::WriteLine(string)
IL_0019: ldloc.0
IL_001a: brtrue.s IL_0023
IL_001c: ldstr "False!"
IL_0021: br.s IL_0028
IL_0023: ldstr "True!"
IL_0028: stloc.2
IL_0029: ldloc.2
IL_002a: call void [mscorlib]System.Console::WriteLine(string)
IL_002f: ret
} // end of method TestApplication::Main
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top