[Mono-list] String comparisons slow

David S buttink at gmail.com
Wed Jul 21 04:18:55 EDT 2010


Man, I wish there was an edit on a Mailing List. ANYWHO, I made my own
string compare

public static void Main()
{
List<List<string>> lists = new List<List<string>>();
Random r = new Random();
for ( int i=0; i < 4; i++ ) {
lists.Add( new List<string>() );
for ( int j=0; j < 400000; j++ )
lists[i].Add( r.Next().ToString() );
}
Stopwatch watch = new Stopwatch();
watch.Start();
foreach ( List<string> list in lists )
list.Sort( (string a, string b) => {
string longer = a;
string shorter = b;
if ( a.Length < b.Length ) {
longer = b;
shorter = a;
}
for ( int i=0; i < shorter.Length; i++ ) {
if ( shorter[i] < longer[i] )
return -1;
else if ( shorter[i] > longer[i] )
return 1;
}
if ( a.Length == b.Length )
return 0;
else
return 1;
});
watch.Stop();
Console.WriteLine( watch.Elapsed );
}

I'm pretty sure that compare is good. But now I get

OUTPUT = 00:00:01.6366606

Then again, I could have messed up my compare so someone check it again for
me. Its not the perfect solution, but its kinda cool that it might be right
and faster than the original. XP Thinking about it, this might only work
with English .... hmmm food for thought.

On Wed, Jul 21, 2010 at 4:06 AM, David S <buttink at gmail.com> wrote:

> Ran the mono profiler which should be taken with a grain of salt but
> whatever
>
>    2639 25.83 % Mono.Globalization.Unicode.CodePointIndexer:ToIndex (int)
>    1921 18.80 % Mono.Globalization.Unicode.SimpleCollator:CompareInternal
> (string,int,int,string,int,int,bool&,bool&,bool,bool,Mono.Globalization.Unicode.SimpleCollator/Context&)
>     824 8.06 % Mono.Globalization.Unicode.SimpleCollator:GetContraction
> (string,int,int,Mono.Globalization.Unicode.Contraction[])
>     505 4.94 % Mono.Globalization.Unicode.MSCompatUnicodeTable:IsIgnorable
> (int,byte)
>     354 3.46 % Mono.Globalization.Unicode.SimpleCollator:Category (int)
>     332 3.25 % Mono.Globalization.Unicode.SimpleCollator:GetContraction
> (string,int,int)
>     311 3.04 % Mono.Globalization.Unicode.SimpleCollator:Compare
> (string,int,int,string,int,int,System.Globalization.CompareOptions)
>     289 2.83 % Mono.Globalization.Unicode.MSCompatUnicodeTable:Category
> (int)
>     233 2.28 % Mono.Globalization.Unicode.SimpleCollator:IsIgnorable
> (int,System.Globalization.CompareOptions)
>     233 2.28 % Mono.Globalization.Unicode.SimpleCollator:FilterOptions
> (int,System.Globalization.CompareOptions)
>     227 2.22 % Mono.Globalization.Unicode.SimpleCollator:GetExtenderType
> (int)
>     178 1.74 % Mono.Globalization.Unicode.SimpleCollator:Level1 (int)
>     168 1.64 % Mono.Globalization.Unicode.SimpleCollator:Level2
> (int,Mono.Globalization.Unicode.SimpleCollator/ExtenderType)
>     167 1.63 %
> Mono.Globalization.Unicode.MSCompatUnicodeTable:HasSpecialWeight (char)
>     156 1.53 % Mono.Globalization.Unicode.MSCompatUnicodeTable:Level3
> (int)
>     156 1.53 % mono()
>     151 1.48 % Mono.Globalization.Unicode.MSCompatUnicodeTable:Level2
> (int)
>     118 1.15 %
>     116 1.14 % Mono.Globalization.Unicode.MSCompatUnicodeTable:Level1
> (int)
>
> Looks like MSCompatUnicodeTable, SimpleCollator, and CodePointIndexer are
> just going to town on it. Well, I don't have the mono source on this comp to
> look into it any further.
>
> OH btw Im using Mono 2.4.4 :)
>
> On Wed, Jul 21, 2010 at 4:00 AM, David S <buttink at gmail.com> wrote:
>
>> I take it back..... DAM YOU MISSING ZERO.
>>
>> for ( int j=0; j < 40000; j++ ) // missing a 0 >.>
>>
>> Output = 00:00:39.8547202 LOL I got pwned.
>>
>>
>> On Wed, Jul 21, 2010 at 3:57 AM, David S <buttink at gmail.com> wrote:
>>
>>> I think there is another problem that is hidden behind all the complexity
>>> of your program. I can tell you this, it isn't string comparer.
>>>
>>> using System;
>>> using System.Collections.Generic;
>>> using System.Diagnostics;
>>>
>>> namespace StringCompare
>>> {
>>>
>>>
>>> public class MainClass
>>>  {
>>>
>>> public static void Main()
>>> {
>>>  List<List<string>> lists = new List<List<string>>();
>>> Random r = new Random();
>>>  for ( int i=0; i < 4; i++ ) {
>>> lists.Add( new List<string>() );
>>>  for ( int j=0; j < 40000; j++ )
>>> lists[i].Add( r.Next().ToString() );
>>>  }
>>> Stopwatch watch = new Stopwatch();
>>> watch.Start();
>>>  foreach ( List<string> list in lists )
>>> list.Sort();
>>> watch.Stop();
>>>  Console.WriteLine( watch.Elapsed );
>>> }
>>> }
>>> }
>>>
>>> OUTPUT = 00:00:03.1364747
>>>
>>> That being said I think its how you set up your threading. It could
>>> very easily be how Mono handles Threads (sleeping and such). Honestly, I
>>> have no idea, but I am intrigued.
>>>
>>> On Wed, Jul 21, 2010 at 3:31 AM, Stifu <stifu at free.fr> wrote:
>>>
>>>>
>>>> No release date, but Miguel recently said [1] "6-8 weeks from now."
>>>> No idea how it compares with the .NET GC.
>>>>
>>>> [1] http://tirania.org/blog/archive/2010/Jul-14.html
>>>>
>>>>
>>>> Mike Christensen-2 wrote:
>>>> >
>>>> > Is there a release date for 2.8?
>>>> >
>>>> > Also, how does the 2.8 GC (I realize it's only a beta) compare with
>>>> the
>>>> > .NET GC?
>>>> >
>>>> > Mike
>>>> >
>>>> > On Tue, Jul 20, 2010 at 11:19 PM, Stifu <stifu at free.fr> wrote:
>>>> >>
>>>> >> There has been performance improvements since Mono 2.0. You may get
>>>> >> better
>>>> >> results with Mono 2.6. Or if you're not in a hurry, wait for Mono 2.8
>>>> >> which
>>>> >> should come out soon (that way you could also check out performances
>>>> with
>>>> >> the new GC).
>>>> >> --
>>>> >> View this message in context:
>>>> >>
>>>> http://mono.1490590.n4.nabble.com/String-comparisons-slow-tp2296525p2296643.html
>>>> >> Sent from the Mono - General mailing list archive at Nabble.com.
>>>> >> _______________________________________________
>>>> >> Mono-list maillist  -  Mono-list at lists.ximian.com
>>>> >> http://lists.ximian.com/mailman/listinfo/mono-list
>>>> >>
>>>> > _______________________________________________
>>>> > Mono-list maillist  -  Mono-list at lists.ximian.com
>>>> > http://lists.ximian.com/mailman/listinfo/mono-list
>>>> >
>>>> >
>>>>
>>>> --
>>>> View this message in context:
>>>> http://mono.1490590.n4.nabble.com/String-comparisons-slow-tp2296525p2296702.html
>>>> Sent from the Mono - General mailing list archive at Nabble.com.
>>>> _______________________________________________
>>>> Mono-list maillist  -  Mono-list at lists.ximian.com
>>>> http://lists.ximian.com/mailman/listinfo/mono-list
>>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20100721/25e21e1d/attachment-0001.html 


More information about the Mono-list mailing list