简体   繁体   English

在C#中,将ulong [64]转换为byte [512]更快?

[英]In C#, convert ulong[64] to byte[512] faster?

I have a way that converts ulongs to bytes using binary shifts in a for statement but it's not very time efficient. 我有一种方法可以使用for语句中的二进制移位将ulongs转换为字节,但它的效率不是很高。 Is there a way to cast a ulong array of size 64 directly into a byte array of size 512? 有没有办法将大小为64的ulong数组直接转换为大小为512的字节数组? This is a section of code that runs thousands of times and I need to save every millisecond so I can in turn save seconds. 这是一段运行数千次的代码,我需要保存每毫秒,以便我可以节省几秒钟。

Edit: Right now this is what I'm doing: 编辑:现在这就是我正在做的事情:

                for (int k = 0; k < ulongs.Length; k++) {
                    bytes[(k << 3)] = (byte)(ulongs[k] >> 56);
                    bytes[(k << 3) + 1] = (byte)(ulongs[k] >> 48);
                    bytes[(k << 3) + 2] = (byte)(ulongs[k] >> 40);
                    bytes[(k << 3) + 3] = (byte)(ulongs[k] >> 32);
                    bytes[(k << 3) + 4] = (byte)(ulongs[k] >> 24);
                    bytes[(k << 3) + 5] = (byte)(ulongs[k] >> 16);
                    bytes[(k << 3) + 6] = (byte)(ulongs[k] >> 8);
                    bytes[(k << 3) + 7] = (byte)(ulongs[k]);
                }
unsafe 
{
    fixed (ulong* src = ulongs) 
    {
        Marshal.Copy(new IntPtr((void*)src), bytes, 0, 512);
    }
}

This seems to work. 这似乎有效。 I'm not sure if fixed is required, but I lost an entire second during timed tests. 我不确定是否需要修复,但在定时测试期间我丢失了一整秒。

Probably the fastest way of doing it is to use unsafe and fixed constructions. 可能最快的方法是使用unsafefixed结构。 Here's an example: 这是一个例子:

ulong[] ulongs = new ulong[64];
byte[] bytes = new byte[512];

unsafe
{
    fixed (ulong* src = ulongs)
    {
        byte* pb = (byte*)src;
        for (int i = 0; i < bytes.Count(); i++)
        {
             bytes[i] = *(pb + i);
        }
    }
}

Please do not forget to use /unsafe compiler switch when compiling the code above. 编译上面的代码时,请不要忘记使用/unsafe编译器开关。

Edit: here's modified version of the first code fragment, which runs much faster on my machine: 编辑:这是第一个代码片段的修改版本,它在我的机器上运行得更快:

unsafe
{
    fixed (ulong* src = ulongs)
    {
        fixed (byte *dst = bytes)
        {
            ulong* pl = (ulong*)dst;
            for (int i = 0; i < ulongs.Count(); i++)
            {
                *(pl + i) = *(src + i);
            }   
        }
    }
}

You may be able to achieve this using a struct and explicit layout instructions using the StructLayout attribute to force variables to occupy the same space. 您可以使用struct和显式布局指令来实现此目的,使用StructLayout属性来强制变量占用相同的空间。

That way you could write the value using the ulong property and read it using the byte array property. 这样你就可以使用ulong属性编写值,并使用byte array属性读取它。 This assumes, of course, that byte array truly takes up the space you expect and that it isn't using 32 bits to hold the 8 bits of information. 当然,这假设byte数组真正占用了您期望的空间,并且它没有使用32位来保存8位信息。 I'll have to look that up in the specification, which I have not done yet. 我将不得不在规范中查看,我还没有完成。

Use the System.Buffer helper class. 使用System.Buffer帮助程序类。

ulong[] buffer = null;
int byteLength = Buffer.ByteLength(buffer);
byte[] byteBuffer = new byte[byteLength];
Buffer.BlockCopy(buffer, 0, byteBuffer, 0, byteLength);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM