473,410 Members | 1,976 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,410 software developers and data experts.

Subtle String Question

In T-SQL, Consider this table:

CREATE TABLE stringTest (
String1 VARCHAR(5),
String2 VARCHAR(5)
)
GO
INSERT StringTest VALUES ('', 'A')
INSERT StringTest VALUES ('A', '')
GO
SELECT
CASE
WHEN String1 > String2
THEN 'String1 Is Greater'
WHEN String2 > String1
THEN "String2 Is Greater
END
GO

Because in T-SQL, it's faster to say:
WHERE String1 > String2
than it is to say:
WHERE String1 <> String2

I believe it's faster because > is SARGable and <> is not. And I only use
this technique when I want to compare to a ZLS, as in

String1 > '' -- faster than String1 <> ''

SOOOOO Then...
Consider this VB Snip:

Private Sub stringGreaterThan()

Dim s1 As String = ""
Dim s2 As String = "A"

Console.WriteLine(Convert.ToBoolean(s1 > s2))
Console.WriteLine(Convert.ToBoolean(s2 > s1))

End Sub

This method yields the same equivalent of it's T-SQL cousin, but does the
performance have the same behavior? i.e.

Are either of these actually faster than the other?

s1 > ""
vs
s1 <> ""

I know I'm nit-picking here, but I have this giant name parser program,
that, under certain conditions needs to sniff individual characters and
strings. I'm already using ByRef and StringBuilder where I can to get
speed, but anything I can conjure up to squeak a few more cycles per
iteration out of this big fat loop would help. 1 ms per iteration processing
a million records would make this thing fly.

--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
Nov 21 '05 #1
5 1053
On 2005-09-23, Mike Labosh <ml*****@hotmail.com> wrote:

Consider this VB Snip:

Private Sub stringGreaterThan()

Dim s1 As String = ""
Dim s2 As String = "A"

Console.WriteLine(Convert.ToBoolean(s1 > s2))
Console.WriteLine(Convert.ToBoolean(s2 > s1))

End Sub

This method yields the same equivalent of it's T-SQL cousin, but does the
performance have the same behavior? i.e.

Are either of these actually faster than the other?
If you're just comparing to the empty string, the fastest is...

If s.Length > 0 Then
....

If comparing two different pre-ordered strings, then you're right...

s1 > s2
is a bit faster than

s1 <> s2

of course, this only works if you know the string order already. If you
have to do two comparisons, then which is faster is entirely dependent
on the strings.
I know I'm nit-picking here,
You are, which is fine, but I strongly suspect you're trying to squeeze
performance out of the wrong place.
but I have this giant name parser program,
that, under certain conditions needs to sniff individual characters and
strings. I'm already using ByRef and
I can't imagine why ByRef would speed you up. If anything I suspect
it's an insignificantly tiny bit slower.
StringBuilder where I can to get
speed, but anything I can conjure up to squeak a few more cycles per
iteration out of this big fat loop would help. 1 ms per iteration processing
a million records would make this thing fly.

Nov 21 '05 #2

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
In T-SQL, Consider this table:

CREATE TABLE stringTest (
String1 VARCHAR(5),
String2 VARCHAR(5)
)
GO
INSERT StringTest VALUES ('', 'A')
INSERT StringTest VALUES ('A', '')
GO
SELECT
CASE
WHEN String1 > String2
THEN 'String1 Is Greater'
WHEN String2 > String1
THEN "String2 Is Greater
END
GO

Because in T-SQL, it's faster to say:
WHERE String1 > String2
than it is to say:
WHERE String1 <> String2

I believe it's faster because > is SARGable and <> is not. And I only use
this technique when I want to compare to a ZLS, as in

String1 > '' -- faster than String1 <> ''

SOOOOO Then...
Consider this VB Snip:

Private Sub stringGreaterThan()

Dim s1 As String = ""
Dim s2 As String = "A"

Console.WriteLine(Convert.ToBoolean(s1 > s2))
Console.WriteLine(Convert.ToBoolean(s2 > s1))

End Sub

This method yields the same equivalent of it's T-SQL cousin, but does the
performance have the same behavior? i.e.

Are either of these actually faster than the other?

s1 > ""
vs
s1 <> ""


You can sometimes get a small improvement by checking the relative lengths
of strings before you compare them.

Private Function IsEqual(ByVal s1 As String, ByVal s2 As String) As Boolean
Return s1.Length = s2.Length AndAlso s1 = s2
End Function

David
Nov 21 '05 #3
> If you're just comparing to the empty string, the fastest is...
If s.Length > 0 Then
Interestingly enough, I expanded my experiment and looked at the MSIL:

// 6 Instructions plus 1 Call
//000010: Dim f1 As Boolean = s1 = s2
IL_000f: ldloc.s s1
IL_0011: ldloc.s s2
IL_0013: ldc.i4.0
IL_0014: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0019: ldc.i4.0
IL_001a: ceq
IL_001c: stloc.0

// 6 Instructions plus 1 Call
//000011: Dim f2 As Boolean = s1 > s2
IL_001d: ldloc.s s1
IL_001f: ldloc.s s2
IL_0021: ldc.i4.0
IL_0022: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0027: ldc.i4.0
IL_0028: cgt
IL_002a: stloc.1

//8 Instructions plus 1 Call
//000012: Dim f3 As Boolean = s1 <> s2
IL_002b: ldloc.s s1
IL_002d: ldloc.s s2
IL_002f: ldc.i4.0
IL_0030: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0035: ldc.i4.0
IL_0036: ceq
IL_0038: ldc.i4.0
IL_0039: ceq
IL_003b: stloc.2

// 4 Instructions plus 1 Virtual Call (whatever that means)
//000013: Dim f4 As Boolean = s2.Length > 0
IL_003c: ldloc.s s2
IL_003e: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0043: ldc.i4.0
IL_0044: cgt
IL_0046: stloc.3

// WE HAVE A WINNER I dug this out of CompilerServices Namespace
// 3 Instructions plus 1 Call
//000014: Dim f5 As Boolean = StringType.StrCmp(s1, s2, False) = -1
IL_0047: ldloc.s s1
IL_0049: ldloc.s s2
IL_004b: ldc.i4.0
IL_004c: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)

--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conquerer.
Kill them all and you're a god." -- Dave Mustane
"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain... On 2005-09-23, Mike Labosh <ml*****@hotmail.com> wrote:

Consider this VB Snip:

Private Sub stringGreaterThan()

Dim s1 As String = ""
Dim s2 As String = "A"

Console.WriteLine(Convert.ToBoolean(s1 > s2))
Console.WriteLine(Convert.ToBoolean(s2 > s1))

End Sub

This method yields the same equivalent of it's T-SQL cousin, but does the
performance have the same behavior? i.e.

Are either of these actually faster than the other?


...

If comparing two different pre-ordered strings, then you're right...

s1 > s2
is a bit faster than

s1 <> s2

of course, this only works if you know the string order already. If you
have to do two comparisons, then which is faster is entirely dependent
on the strings.
I know I'm nit-picking here,


You are, which is fine, but I strongly suspect you're trying to squeeze
performance out of the wrong place.
but I have this giant name parser program,
that, under certain conditions needs to sniff individual characters and
strings. I'm already using ByRef and


I can't imagine why ByRef would speed you up. If anything I suspect
it's an insignificantly tiny bit slower.
StringBuilder where I can to get
speed, but anything I can conjure up to squeak a few more cycles per
iteration out of this big fat loop would help. 1 ms per iteration
processing
a million records would make this thing fly.

Nov 21 '05 #4

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:O2*************@TK2MSFTNGP10.phx.gbl...
If you're just comparing to the empty string, the fastest is...
If s.Length > 0 Then


Interestingly enough, I expanded my experiment and looked at the MSIL:

// 6 Instructions plus 1 Call
//000010: Dim f1 As Boolean = s1 = s2
IL_000f: ldloc.s s1
IL_0011: ldloc.s s2
IL_0013: ldc.i4.0
IL_0014: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0019: ldc.i4.0
IL_001a: ceq
IL_001c: stloc.0

// 6 Instructions plus 1 Call
//000011: Dim f2 As Boolean = s1 > s2
IL_001d: ldloc.s s1
IL_001f: ldloc.s s2
IL_0021: ldc.i4.0
IL_0022: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0027: ldc.i4.0
IL_0028: cgt
IL_002a: stloc.1

//8 Instructions plus 1 Call
//000012: Dim f3 As Boolean = s1 <> s2
IL_002b: ldloc.s s1
IL_002d: ldloc.s s2
IL_002f: ldc.i4.0
IL_0030: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,

string,

bool)
IL_0035: ldc.i4.0
IL_0036: ceq
IL_0038: ldc.i4.0
IL_0039: ceq
IL_003b: stloc.2

// 4 Instructions plus 1 Virtual Call (whatever that means)
//000013: Dim f4 As Boolean = s2.Length > 0
IL_003c: ldloc.s s2
IL_003e: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0043: ldc.i4.0
IL_0044: cgt
IL_0046: stloc.3

// WE HAVE A WINNER I dug this out of CompilerServices Namespace
// 3 Instructions plus 1 Call


You can't judge the relative performance this way. The costs of the invoked
methods could be vastly different.

[mscorlib]System.String::get_Length()

may be 10000x cheaper than

[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,string, bool)David

Nov 21 '05 #5
On 2005-09-23, Mike Labosh <ml*****@hotmail.com> wrote:
If you're just comparing to the empty string, the fastest is...
If s.Length > 0 Then
Interestingly enough, I expanded my experiment and looked at the MSIL:

<snip a bunch of il>
// 4 Instructions plus 1 Virtual Call (whatever that means)
//000013: Dim f4 As Boolean = s2.Length > 0
IL_003c: ldloc.s s2
IL_003e: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0043: ldc.i4.0
IL_0044: cgt
IL_0046: stloc.3

// WE HAVE A WINNER I dug this out of CompilerServices Namespace
// 3 Instructions plus 1 Call
//000014: Dim f5 As Boolean = StringType.StrCmp(s1, s2, False) = -1
IL_0047: ldloc.s s1
IL_0049: ldloc.s s2
IL_004b: ldc.i4.0
IL_004c: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :StrCmp(string,
string,
bool)


That's an awfully silly way to compare things. You don't really care
about the number of instructions in the statement, it's the function
call that's taking all the time.
Nov 21 '05 #6

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

Similar topics

1
by: Skip Montanaro | last post by:
I got to wondering if there is a difference between __contains__() and has_key() for dictionaries (I don't think there is), so I peeked at UserDict.UserDict and saw they were implemented as...
0
by: Brian | last post by:
I've been reading through "Text Processing in Python" by David Mertz. David had a nice phrase in section 1.1 that I think we should turn into a book title. Perhaps we could convince Tim Peters...
7
by: jimspace | last post by:
Can someone look at each of the classes below and tell me where the subtle bugs are? Thanks. Handle/Body idiom class A { private: int i; public:
5
by: The Googler | last post by:
Here is the site in question: http://dev.unmeaningflattery.com/ Open it in Firefox (this problem does not occur in IE). Click on one of the image thumbnails to see the larger image appear...
9
by: bonono | last post by:
Hi, I initially thought that generator/generator expression is cool(sort of like the lazy evaluation in Haskell) until I notice this side effect. >>>a=(x for x in range(2)) >>>list(a) ...
2
by: J | last post by:
Compiling a straight C++ module under VC++ 7 would seem to autogen an 'IJW' type .NET module, even if the module has standard VC++ syntax. I'm assuming that the 'unmanaged' flag would have no...
7
by: Peter Oliphant | last post by:
I use to make the statement that you could always replace any 'private' or 'public' access with 'public' and the code would still compile. This is no longer the case. It turns out that in /clr...
0
by: Johannes Nix | last post by:
There is a subtle bug in the RandomArray module, which is part of the Numeric package. It causes the random number generator RandomArray.normal() to return incorrect values when invoked on Linux...
4
by: William Krick | last post by:
Given these two code snippets, the first generates errors, the second doesn't... // generates errors $tboard = $context; $tboard = empty($tboard)?'':' - '.$tboard; $ttopic = $context; $ttopic...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.