整型数组或结构阵列 - 这是更好? [英] Integer array or struct array - which is better?

查看:228
本文介绍了整型数组或结构阵列 - 这是更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我存储位图数据的二维整数数组( INT [,] )。要访问R,G,B值我用的是这样的:

In my app, I'm storing Bitmap data in a two-dimensional integer array (int[,]). To access the R, G and B values I use something like this:

// read:
int i = _data[x, y];
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);
// write:
_data[x, y] = BitConverter.ToInt32(new byte[] { B, G, R, 0 }, 0);

我使用整型数组,而不是实际的 System.Drawing.Bitmap ,因为我的应用程序在Windows Mobile设备,其中可用于创建位图的内存是非常有限的运行。

I'm using integer arrays instead of an actual System.Drawing.Bitmap because my app runs on Windows Mobile devices where the memory available for creating bitmaps is severely limited.

我不知道,但是,它是否会更有意义宣布这样的结构:

I'm wondering, though, if it would make more sense to declare a structure like this:

public struct RGB
{
    public byte R;
    public byte G;
    public byte B;
}

...然后使用 INT 数组的 RGB 的数组来代替。这样一来,我可以很容易地读写分离的R,G和B值,而不必做位转移和BitConverter-ING。我依稀记得当年的天一些关于字节变量是块排列在32位系统,使字节实际占用4字节的内存,而不是仅仅1(但也许这只是一个Visual Basic的事情)。

... and then use an array of RGB instead of an array of int. This way I could easily read and write the separate R, G and B values without having to do bit-shifting and BitConverter-ing. I vaguely remember something from days of yore about byte variables being block-aligned on 32-bit systems, so that a byte actually takes up 4 bytes of memory instead of just 1 (but maybe this was just a Visual Basic thing).

请问使用结构数组(如 RGB example`以上)比使用int数组更快,它会使用3/4的内存或3整数时代的记忆?

Would using an array of structs (like the RGB example` above) be faster than using an array of ints, and would it use 3/4 the memory or 3 times the memory of ints?

推荐答案

如果您是对的速度的,然后的技术上的我期望 INT [] 版本要快,因为有一个特定的IL指令获取的 INT 从数组(请参阅欧普codes.Ldelem_I4 )。要实现自定义的结构它得到的地址(欧普codes.Ldelema ),然后复制结构(欧普codeS .Ldobj ) - 处理元数据类型为这些步骤

If you are on about speed, then technically I would expect the int[] version to be faster, as there is a specific IL instruction for getting an int from an array (see OpCodes.Ldelem_I4). To do a custom struct it has to get the address (OpCodes.Ldelema) and then copy the struct (OpCodes.Ldobj) - processing the type metadata for both of those steps.

在短期 - 的 INT 办法应该有更好的优化。但是,这是的微优化的 - 中的一般的preFER,使你的code更易读的版本。你的可能是什么的考虑是从 INT 写结构具有自定义静态隐式转换操作符的结构 - 那么你可以有 INT [] 现在仍然如此:

In short - the int approach should have better optimisations. But this is micro-optimisation - in general prefer the version that makes your code more readable. What you might consider is writing the struct with a custom static implicit conversion operator from int to your struct - then you can have the int[] and still do:

MyColor col = intArr[12];

(它会做当然是一个静态调用在中间,)

(it'll do a static call in the middle, of course)

您也可以考虑使用的联合的,所以你不需要做大量的转移:

You might also consider using a union so you don't need to do lots of shifting:

重要我没有理智检查字节序的这一点;只是改变R / G / B的偏移量来改变它。

IMPORTANT I haven't sanity checked the endianness on this; just change the offsets of R/G/B to change it.

class Program
{
    static void Main()
    {
        int[] i = { -1 };
        RGB rgb = i[0];
    }
}
[StructLayout( LayoutKind.Explicit)]
public struct RGB
{
    public RGB(int value) {
        this.R = this.G = this.B = 0; this.Value = value;
    }
    [FieldOffset(0)]
    public int Value;
    [FieldOffset(2)]
    public byte R;
    [FieldOffset(1)]
    public byte G;
    [FieldOffset(0)]
    public byte B;

    public static implicit operator RGB(int value) {
        return new RGB(value);
    }
    public static implicit operator int(RGB value) {
        return value.Value;
    }
}

这篇关于整型数组或结构阵列 - 这是更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆