zoukankan      html  css  js  c++  java
  • For、Foreach、和Parallel.For等简单的速度检测

    控制台代码  直接复制即可

    static void Main(string[] args)
            {
    
                    List<int> testData = new List<int>();
                    Random Rand = new Random();
                    
                    for (int i = 0; i < 1000000; i++)
                    {
                        testData.Add(Rand.Next(1000));
                    }
               
               
                    Console.WriteLine(testData.Sum());
    
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine();
                    TestFor(testData);
                    TestParallelFor(testData);
                    TestParallelForeach(testData);
                }
                Console.ReadKey();
                
            }
    
            static void TestFor(List<int> testData)
            {
                DateTime time1 = DateTime.Now;
                foreach (var item in testData)
                {
                    item.ToString();
                }
                Console.WriteLine(string.Format("ForEach:     t{0} in {1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
            }
    
            static void TestParallelFor(List<int> testData)
            {
                DateTime time1 = DateTime.Now;
                Parallel.For(0, testData.Count, (i, loopState) =>
                {
                    testData[i].ToString();
                });
                Console.WriteLine(string.Format("Parallel.For:   t{0} in {1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
            }
    
            static void TestParallelForeach(List<int> testData)
            {
                
                DateTime time1 = DateTime.Now;
                Parallel.ForEach(testData, (item, loopState) =>
                {
                    item.ToString();
                });
                Console.WriteLine(string.Format("Parallel.ForEach:t{0} in {1}", testData.Sum(), (DateTime.Now - time1).TotalMilliseconds));
            }
  • 相关阅读:
    不定方程(Exgcd)
    [模板]乘法逆元
    STL-Deque(双端队列)与单调队列的实现
    最优得分 score
    摆书 book
    [模板]树链剖分
    [模板]Splay
    NOIP2013 货车运输
    Java的类类型和类的动态加载
    Java:搜索特定后缀名的文件
  • 原文地址:https://www.cnblogs.com/fangyyy/p/10331038.html
Copyright © 2011-2022 走看看