Bitmap.SetPixel acts slower in f# than in c#

…衆ロ難τιáo~ 提交于 2019-12-22 03:24:46

问题


The f# code goes literally 500 times slower than the c# code. What am I doing wrong? I tried to make the code basically the same for both languages. It doesn't make sense that SetPixel would be that much slower in f#.

F#:

module Imaging
open System.Drawing;
#light
type Image (width : int, height : int) = class
  member z.Pixels = Array2D.create width height Color.White

  member z.Width with get() = z.Pixels.GetLength 0

  member z.Height with get() = z.Pixels.GetLength 1

  member z.Save (filename:string) =     
    let bitmap = new Bitmap(z.Width, z.Height)
    let xmax = bitmap.Width-1
    let ymax = bitmap.Height-1
    let mutable bob = 0;
    for x in 0..xmax do
      for y in 0..ymax do
        bitmap.SetPixel(x,y,z.Pixels.[x,y])
    bitmap.Save(filename)

  new() = Image(1280, 720)
end
let bob = new Image(500,500)
bob.Save @"C:\Users\White\Desktop\TestImage2.bmp"

C#:

using System.Drawing;

namespace TestProject
{
public class Image
{

    public Color[,] Pixels;
    public int Width
    {
        get
        {
            return Pixels.GetLength(0);
        }
    }
    public int Height
    {
        get
        {
            return Pixels.GetLength(1);
        }
    }

    public Image(int width, int height)
    {
        Pixels = new Color[width, height];
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                Pixels[x, y] = Color.White;
            }
        }
    }

    public void Save(string filename)
    {
        Bitmap bitmap = new Bitmap(Width, Height);
        for (int x = 0; x < bitmap.Width; x++)
        {
            for (int y = 0; y < bitmap.Height; y++)
            {
                bitmap.SetPixel(x, y, Pixels[x, y]);
            }
        }
        bitmap.Save(filename);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Image i = new Image(500, 500);
        i.Save(@"C:\Users\White\Desktop\TestImage2.bmp");
    }
}
}

回答1:


Your definition of the Pixels property in F# is wrong: each time its value is accessed (e.g. in the inner loop of Save), the definition will be reevaluated. You should use this form instead:

member val Pixels = Array2D.create width height Color.White

This will evaluate the right-hand side exactly once, when the constructor is called, and then cache the value.



来源:https://stackoverflow.com/questions/18341505/bitmap-setpixel-acts-slower-in-f-than-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!