C# LockBits perfomance (int[,] to byte[])

后端 未结 5 1278
滥情空心
滥情空心 2020-12-21 06:26
Graphics g;
using (var bmp = new Bitmap(_frame, _height, PixelFormat.Format24bppRgb))
{
    var data = bmp.LockBits(new Rectangle(0, 0, _frame, _height), ImageLockMo         


        
5条回答
  •  借酒劲吻你
    2020-12-21 06:33

    My understanding is that multidimentional (square) arrays are pretty slow in .Net. You might try changing your _values array to be a single dimension array instead. Here is one reference, there are many more if you search: http://odetocode.com/articles/253.aspx

    Array perf example.

    using System;
    using System.Diagnostics;
    
    class Program
    {
    static void Main(string[] args)
    {
        int w = 1000;
        int h = 1000;
    
        int c = 1000;
    
        TestL(w, h);
        TestM(w, h);
    
    
        var swl = Stopwatch.StartNew();
        for (int i = 0; i < c; i++)
        {
            TestL(w, h);
        }
        swl.Stop();
    
        var swm = Stopwatch.StartNew();
        for (int i = 0; i < c; i++)
        {
            TestM(w, h);
        }
        swm.Stop();
    
        Console.WriteLine(swl.Elapsed);
        Console.WriteLine(swm.Elapsed);
        Console.ReadLine();
    }
    
    
    static void TestL(int w, int h)
    {
        byte[] b = new byte[w * h];
        int q = 0;
        for (int x = 0; x < w; x++)
            for (int y = 0; y < h; y++)
                b[q++] = 1;
    }
    
    static void TestM(int w, int h)
    {
        byte[,] b = new byte[w, h];
    
        for (int y = 0; y < h; y++)
            for (int x = 0; x < w; x++)
                b[y, x] = 1;
    }
    }
    

提交回复
热议问题