Hi!

I have a simple doubt abt the performance when comparing variables with
constats values..

Can any one tell me whether to use
1.
int a;
if(a==10)
{
MessageBox.Show("done");
}

Or

2.
int a;
if(10==a)
{
MessageBox.Show("done");
}

for comparing a variable.. which one will be wise to use if we are
considering performance to be critical and why??

thanks,
Baren

Re: performance - Comparison by john

john
Thu Jan 12 05:22:18 CST 2006

Baren wrote:
> Hi!
>
> I have a simple doubt abt the performance when comparing variables with
> constats values..
>
> Can any one tell me whether to use
> 1.
> int a;
> if(a==10)
> {
> MessageBox.Show("done");
> }
>
> Or
>
> 2.
> int a;
> if(10==a)
> {
> MessageBox.Show("done");
> }
>
> for comparing a variable.. which one will be wise to use if we are
> considering performance to be critical and why??
>
> thanks,
> Baren
It doesn't matter, both should produce basically the same IL, but with 2
lines reversed... If you have performance issues, I doubt they're caused
by an integer comparison - that should be quite fast. Perhaps you could
profile your code to see.

Re: performance - Comparison by lasse

lasse
Thu Jan 12 05:25:29 CST 2006

Hello Baren,

> Hi!
>
> I have a simple doubt abt the performance when comparing variables
> with constats values..
<snip>

Int32 a = 5;
0000003d mov dword ptr [ebp-44h],5
Boolean b1 = a == 10;
00000044 cmp dword ptr [ebp-44h],0Ah * comparison #1
00000048 sete al
0000004b movzx eax,al
0000004e mov dword ptr [ebp-48h],eax
Boolean b2 = 10 == a;
00000051 cmp dword ptr [ebp-44h],0Ah * comparison #2
00000055 sete al
00000058 movzx eax,al
0000005b mov dword ptr [ebp-4Ch],eax

Equal code, same performance. Could be different if the object in a overloads
the equality operator, or is a class (in which the null-issue might crop
up), but in your example it will have the same runtime performance.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@vkarlsen.no
PGP KeyID: 0x2A42A1C



Re: performance - Comparison by Kevin

Kevin
Thu Jan 12 09:05:28 CST 2006

Comparisons are performed mathematically. In each case, the variable "a" is
resolved to a number, and one number is subtracted from the other. If the
result is 0, the return value is true. Let's say that "a" is equal to 11.
Which is faster? Subtracting 11 from 10, or subtracting 10 from 11?
Obviously, both operations are the same.

In other words, in both cases, exactly the same work is performed in exactly
the same way. When this is the case, there is no difference in performance.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Baren" <Baren@discussions.microsoft.com> wrote in message
news:E9CE846C-A28B-4124-9DA3-103F6EE0A5F3@microsoft.com...
> Hi!
>
> I have a simple doubt abt the performance when comparing variables with
> constats values..
>
> Can any one tell me whether to use
> 1.
> int a;
> if(a==10)
> {
> MessageBox.Show("done");
> }
>
> Or
>
> 2.
> int a;
> if(10==a)
> {
> MessageBox.Show("done");
> }
>
> for comparing a variable.. which one will be wise to use if we are
> considering performance to be critical and why??
>
> thanks,
> Baren