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

推荐订阅源

小众软件
小众软件
博客园 - Franky
罗磊的独立博客
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
V
V2EX
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
U
Unit 42
GbyAI
GbyAI
A
About on SuperTechFans
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
D
DataBreaches.Net
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog

博客园 - 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);
            }

        }