使用new关键字多个分配单个位图对象

时间:2014-08-21 04:20:48

标签: c# bitmap compact-framework

我在一个表单中有两种不同的逻辑设计(使用C#)。它们如下:

首先:

//Declaration is only one time
Bitmap a;

//This part of the code will be called many times
reload()
{
    if(z==1)
    {
        //x and y are just strings representing the image path
        a = new Bitmap(x);
        pictureBox1 = a;
    }
    else
    {
        a = new Bitmap(y);
        pictureBox1 = a;
    }
}

第二

//Declaration is only one time
Bitmap a;
Bitmap b;

a = new Bitmap(x);
b = new Bitmap(y);

//This part of the code will be called many times
reload()
{
    if(z==1)
    {
        //x and y are just strings representing the image path
        pictureBox1 = a;
    }
    else
    {
        pictureBox1 = b;
    }
}

我的问题是哪一个更有效率?我正在开发一个内存有限的嵌入式系统(wince 6.0,CF 3.5),我需要处理单个表单中的大量图像(方法二将导致我声明很多Bitmap对象)。

请建议,谢谢。

1 个答案:

答案 0 :(得分:0)

假设这些足够大Bitmap s,为了提高内存效率,第一个更好。它做同样的工作,同时允许有Bitmap而不是两个实例。

现在问题变成了,您是否愿意花费内存开销并避免重复实例化它们?我经常发现,在嵌入式系统中,RAM和CPU时间都非常有限,而且无论你是否使用RAM来节省CPU时间,反之亦然,这实际上取决于你所做的事情。

另一个与你相反的例子,我曾经在学校编写了一个嵌入式音乐合成器。 CPU时间至关重要。所以说我想创造一个平滑的基调。最好是将信封插入RAM进行采样而不是计算正弦值。