Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 哪种编组方法更好?_C#_Bytearray_Structure_Marshalling - Fatal编程技术网

C# 哪种编组方法更好?

C# 哪种编组方法更好?,c#,bytearray,structure,marshalling,C#,Bytearray,Structure,Marshalling,我找到了两种将字节[]转换为结构的方法。但我不知道这两种方法之间是否有任何区别?谁能知道哪一个更好(性能…) #1: public static T ByteArrayToStructure<T>(byte[] buffer) { int length = buffer.Length; IntPtr i = Marshal.AllocHGlobal(length); Marshal.Copy(buffer, 0, i, length); T resul

我找到了两种将
字节[]
转换为结构的方法。但我不知道这两种方法之间是否有任何区别?谁能知道哪一个更好(性能…)

#1:

public static T ByteArrayToStructure<T>(byte[] buffer)
{
    int length = buffer.Length;
    IntPtr i = Marshal.AllocHGlobal(length);
    Marshal.Copy(buffer, 0, i, length);
    T result = (T)Marshal.PtrToStructure(i, typeof(T));
    Marshal.FreeHGlobal(i);
    return result;
}
public static T ByteArrayToStructure<T>(byte[] buffer)
{
    GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return result;
}
公共静态T字节数组结构(字节[]缓冲区)
{
int length=buffer.length;
IntPtr i=封送。AllocHGlobal(长度);
封送处理副本(缓冲区,0,i,长度);
T result=(T)Marshal.PtrToStructure(i,typeof(T));
弗里赫全球元帅(一);
返回结果;
}
#2:

public static T ByteArrayToStructure<T>(byte[] buffer)
{
    int length = buffer.Length;
    IntPtr i = Marshal.AllocHGlobal(length);
    Marshal.Copy(buffer, 0, i, length);
    T result = (T)Marshal.PtrToStructure(i, typeof(T));
    Marshal.FreeHGlobal(i);
    return result;
}
public static T ByteArrayToStructure<T>(byte[] buffer)
{
    GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return result;
}
公共静态T字节数组结构(字节[]缓冲区)
{
GCHandle=GCHandle.Alloc(缓冲区,GCHandleType.pinted);
T result=(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
返回结果;
}

我使用以下代码为您做了一个基准测试:

const int ILITERATIONS = 10000000;

const long testValue = 8616519696198198198;
byte[] testBytes = BitConverter.GetBytes(testValue);

// warumup JIT
ByteArrayToStructure1<long>(testBytes);
ByteArrayToStructure2<long>(testBytes);

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

for (int i = 0; i < ILITERATIONS; i++)
{
    ByteArrayToStructure1<long>(testBytes);
}

stopwatch.Stop();
Console.WriteLine("1: " + stopwatch.ElapsedMilliseconds);

stopwatch.Reset();

stopwatch.Start();

for (int i = 0; i < ILITERATIONS; i++)
{
    ByteArrayToStructure2<long>(testBytes);
}

stopwatch.Stop();
Console.WriteLine("2: " + stopwatch.ElapsedMilliseconds);

stopwatch.Reset();

stopwatch.Start();

for (int i = 0; i < ILITERATIONS; i++)
{
    BitConverter.ToInt64(testBytes, 0);
}

stopwatch.Stop();
Console.WriteLine("3: " + stopwatch.ElapsedMilliseconds);

Console.ReadLine();

在什么方面更好?顺便说一句,我认为第二个没有达到你的期望。你测试过吗?如果是这样,请向我们展示您用于确保方法按预期工作的代码。性能更好(我编辑了我的帖子)。你确定2号不行吗?好的。所以有一些区别。你能说得更多吗?如果你想在你的特定情况下比较性能,你必须自己测量。关于
Marshal.AllocHGlobal()
vs
GCHandle.Alloc()
的问题,对于这个特定情况,它相当有趣,不值得标准的“测量你自己”简介。旁注:确保有热身电话排除JIT,看看你是否真的调用了两种不同的方法…@AlexeiLevenkov谢谢你的建议,更新了我的答案:)