473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What if structure is faster? or not?>

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";

Nov 18 '05 #1
3 1135
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
Nov 18 '05 #2
An extra question so i can do some more testing..

how do you view the IL?

"EijiTek" <ei*****@comcast.net> wrote in message
news:3-********************@comcast.com...
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

Nov 18 '05 #3
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" <dc******@hotmail.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
An extra question so i can do some more testing..

how do you view the IL?

"EijiTek" <ei*****@comcast.net> wrote in message
news:3-********************@comcast.com...
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


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sylvia | last post by:
Hi, I'm trying to render a XML structure to HTML using XSLT. My XML describe the header of a table with a complex and not linear structure. The first row of the header table always contains the...
4
by: Theodore H. Smith | last post by:
OK, so I've invented a new data structure, used for look up tables (AKA "map" or "dictionary"), which is much faster, and uses less RAM, than the popular hashing algorithms. I've written a...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
1
by: Jonathan Amend | last post by:
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to add the pointer of a structure which includes an array to an array and then pass it to an API. Here are the two code...
14
by: teslar91 | last post by:
As a fairly new .NET coder, I would greatly appreciate some comments on any .NET classes that are known to be notoriously slow by comparison to direct API calls. I've already had a bad...
11
by: JerryWEC | last post by:
I want to be able to create a structure like I did in VB6, that have variables Name As String * 10 Age As String * 3 for the size of the strings. Can I do this some way in VB.net? I'm...
5
by: Dominik Czechowski | last post by:
Hi everyone! I have two tables, T1 and T2 defined as follows: create table T1(c1 integer not null, primary key (c1)); create table T2(c1 integer not null, c2 varchar(100) not null, primary...
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.