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

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

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
 
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
 
* "Rich said:
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".
 
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
 
* "Rich said:
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.
 
Back
Top