Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

#if true
            const int MaxNum = 1024*100;
            keyValue[] keyvalues = new keyValue[MaxNum];

            System.Random random = new Random();
            for(int i=0; i<MaxNum; ++i){
                int len = random.Next()%12+4;
                int notAlnum = len/3;
                keyvalues[i].key_ = System.Web.Security.Membership.GeneratePassword(len, notAlnum);
                keyvalues[i].value_ = string.Format("{0}", i);
            }

            for(int count = 0; count < 2; ++count) {
                {
                    //TinyCollections
                    TinyCollections.Hopscotch<string, string> strToStr = new TinyCollections.Hopscotch<string, string>();
                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        strToStr.Overwrite(keyvalues[i].key_, keyvalues[i].value_);
                    }

                    for(int i = MaxNum - 2; 0 <= i; i -= 2) {
                        strToStr.Remove(keyvalues[i].key_);
                    }

                    for(int i = 0; i < MaxNum; i += 2) {
                        strToStr.Overwrite(keyvalues[i].key_, keyvalues[i].value_);
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Tiny:InsertRemove " + stopwatch.Elapsed);

                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        string value;
                        if(strToStr.TryGetValue(keyvalues[i].key_, out value) && value == keyvalues[i].value_) {
                        } else {
                            System.Console.WriteLine("error");
                        }
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Tiny:Find {0} Memory {1}", stopwatch.Elapsed, GC.GetTotalMemory(true));
                }
                {
                    //System.Collections.Generic.Dictionary
                    Dictionary<string, string> direcionary = new Dictionary<string, string>();
                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        direcionary[keyvalues[i].key_] = keyvalues[i].value_;
                    }

                    for(int i = MaxNum - 2; 0 <= i; i -= 2) {
                        direcionary.Remove(keyvalues[i].key_);
                    }

                    for(int i = 0; i < MaxNum; i += 2) {
                        direcionary[keyvalues[i].key_] = keyvalues[i].value_;
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Dictionary:InsertRemove " + stopwatch.Elapsed);

                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        string value;
                        if(direcionary.TryGetValue(keyvalues[i].key_, out value) && value == keyvalues[i].value_) {
                        } else {
                            System.Console.WriteLine("error");
                        }
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Dictionary:Find {0} Memory {1}", stopwatch.Elapsed, GC.GetTotalMemory(true));
                }
            }
#else
            const int MaxNum = 1024*100;
            keyValueInt[] keyvalues = new keyValueInt[MaxNum];
            System.Random random = new System.Random();

            for(int i=0; i<MaxNum; ++i){
                keyvalues[i].key_ = random.Next();
                keyvalues[i].value_ = i;
            }

            for(int count=0; count<2; ++count) {
                {
                    //TinyCollections
                    TinyCollections.HashMap<int, int> strToStr = new HashMap<int, int>();
                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        strToStr.Overwrite(keyvalues[i].key_, keyvalues[i].value_);
                    }

                    for(int i = MaxNum - 2; 0 <= i; i -= 2) {
                        strToStr.Remove(keyvalues[i].key_);
                    }

                    for(int i = 0; i < MaxNum; i += 2) {
                        strToStr.Overwrite(keyvalues[i].key_, keyvalues[i].value_);
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Tiny:InsertRemove " + stopwatch.Elapsed);

                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        int value;
                        if(strToStr.TryGetValue(keyvalues[i].key_, out value) && value == keyvalues[i].value_) {
                        } else {
                            System.Console.WriteLine("error");
                        }
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Tiny:Find " + stopwatch.Elapsed);
                }

                {
                    //System.Collections.Generic.Dictionary
                    Dictionary<int, int> direcionary = new Dictionary<int, int>();
                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        direcionary[keyvalues[i].key_] = keyvalues[i].value_;
                    }

                    for(int i = MaxNum - 2; 0 <= i; i -= 2) {
                        direcionary.Remove(keyvalues[i].key_);
                    }

                    for(int i = 0; i < MaxNum; i += 2) {
                        direcionary[keyvalues[i].key_] = keyvalues[i].value_;
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Dictionary:InsertRemove " + stopwatch.Elapsed);

                    stopwatch.Reset();
                    stopwatch.Start();

                    for(int i = 0; i < MaxNum; ++i) {
                        int value;
                        if(direcionary.TryGetValue(keyvalues[i].key_, out value) && value == keyvalues[i].value_) {
                        } else {
                            System.Console.WriteLine("error");
                        }
                    }
                    stopwatch.Stop();
                    System.Console.WriteLine("Dictionary:Find " + stopwatch.Elapsed);
                }
                System.GC.Collect();
            }
#endif
        }