惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

博客园 - Diego

MongoDb playing ground: run as service, sharding. 还是等吧 Notes for these past days: Python, Android, MongoDb, and WebLoad. One day configuring network in Ubuntu 10.4 - Diego Only if I can feel the heat from soccer- steady the first day. Finally, World Cup comes to be the evidence of my 2010. 2010 Fifa world cup time table. Interesting memo for i++ and ++i. Learn at least a half dozen programming languages. (extracted) System.IO.IOException: The semaphore timeout period has expired. - Diego Command to restart/shut down remotely. SVN authentication... Producing function in SQL. Tip 2: Code Review Tools... to be a team. Command to check Group Policy. Exception doesn't travel across threads... Resume to be along with the technical forums. How To Get Customer's Screen Pixel. Some notes when transferring from Web 1.1 to Web 2.0
Performance issue, be cautious to use List.Contains...
Diego · 2010-04-29 · via 博客园 - Diego

1. Big difference between having contains and not having, and no overriding in PerformanceInfo:

[10-05-03 11:32:25.740][Info]Program.TestCollection(): Cost 00:00:00.0312488 ([AppName:ConsoleTest.exe])
[10-05-03 11:32:59.004][Info]Program.TestCollection(): Cost 00:00:33.3737184 ([AppName:ConsoleTest.exe])
[10-05-03 11:32:59.035][Info]Program.TestCollection(): Cost 00:00:00.0312488 ([AppName:ConsoleTest.exe])
[10-05-03 11:32:59.082][Info]Program.TestCollection(): Cost 00:00:00.0468732 ([AppName:ConsoleTest.exe])

2. Try List.Distinct or use Dictionary if we want to check Contains...

 

Sample codes below:

        static void Main(string[] args)

        {
            int count = 100000;
            DateTime dt0 = DateTime.Now;
            TestCollectionWithoutContains(count);
            DateTime dt1 = DateTime.Now;
            Log.Info("Cost "+ (dt1 - dt0));
            TestCollectionUsingListContains(count/10);
            DateTime dt2 = DateTime.Now;
            Log.Info("Cost " + (dt2 - dt1));
            TestCollectionUsingDicContains(count);
            DateTime dt3 = DateTime.Now;
            Log.Info("Cost " + (dt3 - dt2));
            TestCollectionUsingListDistinct(count);
            DateTime dt4 = DateTime.Now;
            Log.Info("Cost " + (dt4 - dt3));

    }

        private static void TestCollectionUsingListDistinct(int count)
        {
            List<string> list = new List<string>();
            for (int j = 0; j < count; j++)
            {
                string id = (j / 2).ToString();
                list.Add(id);
            }
            List<string> list2 = new List<string>();
            foreach (string pi in list.Distinct<string>())
            {
                list2.Add(pi);
            }
        }

        private static void TestCollectionUsingListContains(int count)
        {
            List<string> list = new List<string>();
            for (int j = 0; j < count; j++)
            {
                string id = (j/2).ToString();
                if (!list.Contains(id))
                {
                    list.Add(id);
                }
            }
        }

        private static void TestCollectionUsingDicContains(int count)
        {
            //List<PerformanceInfo> list = new List<PerformanceInfo>();
            Dictionary<string, string> list = new Dictionary<string, string>();
            for (int j = 0; j < count; j++)
            {
                string id = (j / 2).ToString();
                if (!list.ContainsKey(id))
                {
                    list.Add(id, j.ToString());
                }
            }
        }

        private static void TestCollectionWithoutContains(int count)
        {
            List<string> list = new List<string>();
            for (int j = 0; j < count; j++)
            {
                string id = (j / 2).ToString();
                list.Add(id);
            }

        }