zoukankan      html  css  js  c++  java
  • 编程之美——差之毫厘

    先看这一篇文章http://yishan.cc/blogs/gpww/archive/2009/11/03/locality-and-false-sharing.aspx

    在尝试运行下面代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading.Tasks;
    
    namespace TestSum
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 1 << 10;
                int[,] array = new int[n, n];
    
                for (int x = 0; x < n; x++)
                    for (int y = 0; y < n; y++)
                        array[x, y] = 1;
                Test(SumA, array, n);
                Test(SumB, array, n);
                Test(SumC, array, n);
                Test(SumD, array, n);
                Console.ReadKey();
            }
    
            static void Test(SumDele Sum, int[,] array, int n)
            {
                int sum = 0;
                Stopwatch watch = new Stopwatch();
                watch.Start();
                for (int i = 0; i < n; i++) sum += Sum(array, n);
                watch.Stop();
                Console.WriteLine("{0}: {1}, {2}s",
                Sum.Method.Name, sum, watch.Elapsed.TotalSeconds);
            }
    
            delegate int SumDele(int[,] array, int n); 
    
            static int SumA(int[,] array, int n)
            {
                int sum = 0;
                for (int x = 0; x < n; x++)
                    for (int y = 0; y < n; y++)
                    {
                        sum += array[y, x];
                    }
                return sum;
            }
    
            static int SumB(int[,] array, int n)
            {
                int sum = 0;
                for (int x = 0; x < n; x++)
                    for (int y = 0; y < n; y++)
                    {
                        sum += array[x, y];
                    }
                return sum;
            }
    
            static int SumC(int[,] array, int n)
            {
                int m = Environment.ProcessorCount;
                int[] result = new int[m];
    
                int step = n / m;
                int leftover = n % m;
    
                Parallel.For(0, m, (i) =>
                {
                    int from = i * step;
                    int to = from + step + (i < m - 1 ? 0 : leftover);
    
                    for (int x = from; x < to; x++)
                        for (int y = 0; y < n; y++)
                        {
                            result[i] += array[x, y];
                        }
                });
    
                return result.Sum();
            }
    
            static int SumD(int[,] array, int n)
            {
                int m = Environment.ProcessorCount;
                int[] result = new int[m];
    
                int step = n / m;
                int leftover = n % m;
    
                Parallel.For(0, m, (i) =>
                {
                    int from = i * step;
                    int to = from + step + (i < m - 1 ? 0 : leftover);
                    int temp = 0;
                    for (int x = from; x < to; x++)
                        for (int y = 0; y < n; y++)
                        {
                            temp += array[x, y];
                        }
                    result[ i ] = temp;
                });
    
                return result.Sum();
            }
        }
    }
    
  • 相关阅读:
    PHP双向队列
    [转]数据库查询的3个优化方法
    MySQL性能测试工具 mysqlslap
    PHP各种魔术方法测试
    VBA中级班课时3小结
    VBA中级班课时1小结
    执行cmd并返回程序结果
    Update Dataset data back to Database
    终于会用c#中的delegate(委托)和event(事件)了
    c#: Enqueued event for Queue<T>
  • 原文地址:https://www.cnblogs.com/tewuapple/p/2345289.html
Copyright © 2011-2022 走看看