467,074 Members | 882 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

more efficient - not str1.Equals("xyz") or str1 <> "xyz"?

Hello,

Just checking which is more efficient/better/or correct

Do While something
str1 = dr(i).ToString
If Not str1.Equals("xyz") Then
....

or

If str1 <> "xyz" Then
....

or does it make any difference? or any other syntax?

TIA,
Rich
Nov 20 '05 #1
  • viewed: 1368
Share:
4 Replies
Hi Rich,

This kind of instructions are even when they are a million times in a loop
almost never interesting about efficiency, keep in mind that one show or add
of a visible control can cost you billions more time.

When there was a "better" because of efficiency there would have been not
that much posibilities. So look at good programming where reading for others
is very important before you start optimizing in this part of your program.

I do not like to read statements with str1.Equals("xyz"). That does me think
on languages which no sufficient logical operators and where that has to be
done with methods.

However just a thought,

Cor

Just checking which is more efficient/better/or correct

Do While something
str1 = dr(i).ToString
If Not str1.Equals("xyz") Then
...

or

If str1 <> "xyz" Then
...

or does it make any difference? or any other syntax?

TIA,
Rich

Nov 20 '05 #2
* "Rich" <an*******@discussions.microsoft.com> scripsit:
Just checking which is more efficient/better/or correct

Do While something
str1 = dr(i).ToString
If Not str1.Equals("xyz") Then
...

or

If str1 <> "xyz" Then
...

or does it make any difference? or any other syntax?


Call "ILDASM" on the result to see the "difference".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Very interesting.

With str1.Equals I get this with "ILDASM":

IL_0029: callvirt instance bool [mscorlib]
System.String::Equals(string)
If I use st1 <> "xyz" I get this:

IL_002a: call int32 [Microsoft.VisualBasic]
Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp
(string, string, bool)

It appears from "ILDASM" that str1.Equals is more
efficient overall.

Slowly, I will migrate away from my VB6 habits :).

Thanks,
Rich

-----Original Message-----
* "Rich" <an*******@discussions.microsoft.com> scripsit:
Just checking which is more efficient/better/or correct

Do While something
str1 = dr(i).ToString
If Not str1.Equals("xyz") Then
...

or

If str1 <> "xyz" Then
...

or does it make any difference? or any other syntax?


Call "ILDASM" on the result to see the "difference".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
.

Nov 20 '05 #4
* "Rich" <an*******@discussions.microsoft.com> scripsit:
With str1.Equals I get this with "ILDASM":

IL_0029: callvirt instance bool [mscorlib]
System.String::Equals(string)
If I use st1 <> "xyz" I get this:

IL_002a: call int32 [Microsoft.VisualBasic]
Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp
(string, string, bool)

It appears from "ILDASM" that str1.Equals is more
efficient overall.

Slowly, I will migrate away from my VB6 habits :).


I would still use '<>', even if its some ns slower. I am not sure how
'StrCmp' and 'Equals' are implemented, so it's hard to tell which one is
faster.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by |-|erc | last post: by
7 posts views Thread by =?Utf-8?B?UmljaA==?= | last post: by
20 posts views Thread by liujiaping | last post: by
5 posts views Thread by .Net Sports | last post: by
71 posts views Thread by Jack | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.