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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks 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);
            }

        }