Click here to Skip to main content
15,885,309 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Search Button Pin
MikeK322-Nov-12 11:51
MikeK322-Nov-12 11:51 
GeneralRe: C# Search Button Pin
fjdiewornncalwe2-Nov-12 11:53
professionalfjdiewornncalwe2-Nov-12 11:53 
Questioncase insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 9:31
Blake Miller2-Nov-12 9:31 
AnswerRe: case insensitive string comparison - relative speed Pin
Eddy Vluggen2-Nov-12 9:43
professionalEddy Vluggen2-Nov-12 9:43 
GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:26
Blake Miller2-Nov-12 10:26 
GeneralRe: case insensitive string comparison - relative speed Pin
Eddy Vluggen2-Nov-12 10:29
professionalEddy Vluggen2-Nov-12 10:29 
GeneralRe: case insensitive string comparison - relative speed Pin
SledgeHammer012-Nov-12 11:20
SledgeHammer012-Nov-12 11:20 
AnswerRe: case insensitive string comparison - relative speed Pin
Richard Deeming2-Nov-12 9:47
mveRichard Deeming2-Nov-12 9:47 
The first one creates two new strings, converting each character to lower-case, and then compares the results.

The second one performs a case-insensitive comparison of each character, without allocating any new strings.

Instinct would say that the second will always out-perform the first. Here's some code to test that:
C#
int ITERATIONS = 1000000;

string s1 = "Hello World";
string s2 = "hello world";

Debug.Assert(s1.ToLower() == s2.ToLower());
Debug.Assert(string.Compare(s1, s2, true) == 0);

var sw1 = new Stopwatch();
sw1.Start();
for (int i = 0; i < ITERATIONS; i++)
{
   Debug.Assert(s1.ToLower() == s2.ToLower());
}
sw1.Stop();

var sw2 = new Stopwatch();
sw2.Start();
for (int i = 0; i < ITERATIONS; i++)
{
   Debug.Assert(string.Compare(s1, s2, true) == 0);
}
sw2.Stop();


Console.WriteLine("ToLower: {0}", sw1.Elapsed);
Console.WriteLine("Compare: {0}", sw2.Elapsed);


On my computer, the output is:
ToLower: 00:00:00.4507542
Compare: 00:00:00.1856049


The ToLower approach takes more than twice as long as the Compare approach.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:28
Blake Miller2-Nov-12 10:28 
AnswerRe: case insensitive string comparison - relative speed Pin
Clifford Nelson2-Nov-12 10:18
Clifford Nelson2-Nov-12 10:18 
GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:28
Blake Miller2-Nov-12 10:28 
AnswerRe: case insensitive string comparison - relative speed Pin
jschell2-Nov-12 12:46
jschell2-Nov-12 12:46 
QuestionSpell checker in code Pin
PozzaVecia2-Nov-12 9:11
PozzaVecia2-Nov-12 9:11 
AnswerRe: Spell checker in code Pin
Eddy Vluggen2-Nov-12 9:38
professionalEddy Vluggen2-Nov-12 9:38 
GeneralRe: Spell checker in code Pin
PozzaVecia2-Nov-12 9:44
PozzaVecia2-Nov-12 9:44 
GeneralRe: Spell checker in code Pin
Eddy Vluggen2-Nov-12 10:11
professionalEddy Vluggen2-Nov-12 10:11 
GeneralRe: Spell checker in code Pin
PozzaVecia2-Nov-12 11:22
PozzaVecia2-Nov-12 11:22 
AnswerRe: Spell checker in code Pin
Eddy Vluggen2-Nov-12 11:31
professionalEddy Vluggen2-Nov-12 11:31 
AnswerRe: Spell checker in code Pin
Clifford Nelson2-Nov-12 10:26
Clifford Nelson2-Nov-12 10:26 
GeneralRe: Spell checker in code Pin
PozzaVecia2-Nov-12 11:22
PozzaVecia2-Nov-12 11:22 
QuestionSource file with repetitive data Pin
C-P-User-32-Nov-12 6:02
C-P-User-32-Nov-12 6:02 
AnswerRe: Source file with repetitive data Pin
Richard MacCutchan2-Nov-12 6:29
mveRichard MacCutchan2-Nov-12 6:29 
GeneralRe: Source file with repetitive data Pin
C-P-User-35-Nov-12 8:00
C-P-User-35-Nov-12 8:00 
GeneralRe: Source file with repetitive data Pin
Richard MacCutchan5-Nov-12 22:02
mveRichard MacCutchan5-Nov-12 22:02 
GeneralRe: Source file with repetitive data Pin
C-P-User-36-Nov-12 6:02
C-P-User-36-Nov-12 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.