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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
WordPress大学
WordPress大学
U
Unit 42
I
InfoQ
A
About on SuperTechFans
宝玉的分享
宝玉的分享
J
Java Code Geeks
博客园 - 司徒正美
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
腾讯CDC
Recent Announcements
Recent Announcements

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

        }