From: Andreas B. <an...@se...> - 2006-08-28 12:39:41
|
Hello guys, Serge: Guig.NewGuid() works fine. However I did some test and believe now that GUIDs are not the best solution either because, they seem to be damn slow in our scenario and actually that applies to the string.GetHashCode() thing. As to the GUIDS: I modified the AxiomCollection so that it doesn't use the while (base.ContainsKey(key = GetNextKey())) ; base.Add(key,item); approach and used direct adding without previous ContainsKey() checking: base.Add(GetNextKey(item), item); the string key was constructed in the GetNextKey() method as follows: return (K)Convert.ChangeType(Guid.NewGuid() + typeof(T).Name, typeof(K)); i.e. simply get a Guid and append the current type name of the collection items. I expected a speed up as the while(base.ContainsKey()) check was ommited but was quite shocked, see below. I also tried to construct the key prepending a hash value of the key to it so let's mention it as well: { string s = typeof( T ). Name + checked(nextUniqueKeyCounter++).ToString(); key = ((K) Convert. ChangeType( s. GetHashCode() + s, typeof( K ) ) ); } Results: a) The original while-contains approach measure: 100000 items added in 12.0411 seconds b) The direct adding using a GUID measure: 100000 items added in 59.1695 seconds c) A while-contains approach additionaly prepending the result of GetHashCode() applied with the pre-final string key: 100000 items added in 60.6140 seconds (I think the necessary heap allocations do that...) Come on, just use the simplest key = (K)Convert.ChangeType(checked(++nextUniqueKeyCounter) + typeof(T).Name, typeof(K)); approach. I.e. keys looking like this: "1234Entity" What do you say? In the final still correct thing I got 100000 items added in 11.1862 seconds. Without a while-contains check which I could omit for my simple scenario it was about 9.8 secs. So the ContainsKey() method seems to be really fast. (And, probably, the less chars of the key it has to compare to existing items before equality is determined, the faster. So it would be probably better to have a "4321Entity" key for a 1234 counter :-) however.) Andris ----- Original Message ----- From: "Serge Lobko-Lobanovsky aka arilou" <ser...@gm...> To: "Andreas Bednarek" <an...@se...> Cc: "A list for discussion of Axiom Engine development" <axi...@li...> Sent: Monday, August 28, 2006 8:56 AM Subject: Re: [axiomengine-developers] AxiomCollection.cs design issue > Hello Andreas, > > Actually, it should've been > > Guid g = Guid.NewGuid(); // factory method > Console.WriteLine(g); > Console.ReadLine(); > > Sorry, couldn't test it before posting (no VS.NET at home yet after a > major upgrade :( ) > > Sunday, August 27, 2006, 10:24:22 PM, you wrote: > >> I'm afraid that > >> (new Guid()).ToString(); > >> produces a >> "00000000-0000-0000-0000-000000000000" > >> :( >> (did a test right now ) > |