Mã f # chậm hơn 500 lần so với mã C#. Tôi đang làm gì sai? Tôi đã cố gắng làm cho mã cơ bản giống nhau cho cả hai ngôn ngữ. Nó không có nghĩa là SetPixel sẽ chậm hơn nhiều trong f #.Bitmap.SetPixel hoạt động chậm hơn trong f # so với C#
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");
}
}
}
Bất kỳ lý do nào bạn không muốn sử dụng 'LockBits' ngay từ đầu? Điều đó sẽ hiệu quả hơn đáng kể, tôi nghi ngờ. –
Loại CLR của 'z.Pixels' là gì? Có lẽ một loại f # đặc biệt? – usr
Tôi muốn tạo một mảng 1d trong f # và chuyển đổi thành bitmap trong một lần. Sẽ dễ dàng hơn và rất hiệu quả. –