Arrays 如何从C中的数组执行零拷贝赋值#
好的,所以我正在拼命地研究用C#做一些零拷贝作业。现在的问题是我有一个字节数组,比如说array1。我需要指向该数组中的一个特定位置,现在棘手的部分是实际上不从该特定位置复制字节,直到最后,而必须以某种方式访问这些字节。为了使这一小部分数据可以访问,我必须将它放在另一个字节数组中,比如array2。我不能使用Array.Copy(),因为它将创建数据的副本,现在我必须使用不安全/固定的构造并指向数据的特定部分。简单地说,无需复制即可通过另一个数组或其他东西访问现有数据。我似乎缺乏那种魔力的诀窍Arrays 如何从C中的数组执行零拷贝赋值#,arrays,Arrays,好的,所以我正在拼命地研究用C#做一些零拷贝作业。现在的问题是我有一个字节数组,比如说array1。我需要指向该数组中的一个特定位置,现在棘手的部分是实际上不从该特定位置复制字节,直到最后,而必须以某种方式访问这些字节。为了使这一小部分数据可以访问,我必须将它放在另一个字节数组中,比如array2。我不能使用Array.Copy(),因为它将创建数据的副本,现在我必须使用不安全/固定的构造并指向数据的特定部分。简单地说,无需复制即可通过另一个数组或其他东西访问现有数据。我似乎缺乏那种魔力的诀窍
byte[] array1 ---> contains data say 10 elements
byte[] array2 ---> This must have the data from array1 from element say, 2-8 without
copying the data from array1
非常感谢您的帮助
谢谢
(还有,这可能吗?)
好的,所以我做了一些基准测试,最后通过了一些障碍。多亏了关于安排的建议,它解决了我的大部分问题
结果如下(根据我的要求发布)
使用Core2Duo-2.53GHz 2GB内存,在.NET3.5(C#,VS2008)下运行
我是一个比几分钟前更快乐的开发者。为Dtb干杯
代码如下。如果您指出任何缺陷,我们将不胜感激
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ArrayCopyArraySementProfiling
{
class Program
{
public static Stopwatch stopWatch = new Stopwatch();
public static TimeSpan span = new TimeSpan();
public static double totalTime = 0.0;
public static int iterations = 1000000;
static void Main(string[] args)
{
int size = 1000;
int startIndex = 0;
int endIndex = 99;
byte[] array1 = new byte[size];
byte[] array2 = new byte[endIndex - startIndex + 1];
for (int index = startIndex; index < size ; index++)
{
array1[index] = (byte)index;
}
ArraySegment<byte> arraySeg;
for (int index = 0; index < iterations; index++)
{
stopWatch.Start();
arraySeg = new ArraySegment<byte>(array1, startIndex, endIndex);
stopWatch.Stop();
totalTime += stopWatch.Elapsed.TotalMilliseconds;
}
Console.WriteLine("ArraySegment:{0:F6}", totalTime / iterations);
stopWatch.Reset();
totalTime = 0.0;
for (int index = 0; index < iterations; index++)
{
stopWatch.Start();
Array.Copy(array1, startIndex, array2, 0, endIndex);
stopWatch.Stop();
totalTime += stopWatch.Elapsed.TotalMilliseconds;
}
Console.WriteLine("Array.Copy:{0:F6}", totalTime / iterations);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
命名空间ArrayCopy阵列配置
{
班级计划
{
公共静态秒表秒表=新秒表();
公共静态时间跨度=新时间跨度();
公共静态双总时间=0.0;
公共静态整数迭代次数=1000000;
静态void Main(字符串[]参数)
{
int size=1000;
int startIndex=0;
int-endIndex=99;
字节[]数组1=新字节[大小];
字节[]数组2=新字节[endIndex-startIndex+1];
for(int index=startIndex;index
在.NET中,数组不能指向另一个数组。您可以做的是沿着array1
传递一个偏移量和长度,这样收件人就可以从该偏移量中读取给定长度的数组,而不是从0
开始读取array.length
看一看这部电影。嗯,它看起来很有前途。几乎是我需要的。“新”这个关键词真的要了我的命。它会让事情变慢,这其实是我不想要的。不过,我必须对Array.Copy()进行一些分析。谢谢你,干杯!如果它真的要了你的命,你怎么还在打字?僵尸@乔纳森,我能说什么?我想念C的速度!因为C还活着!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ArrayCopyArraySementProfiling
{
class Program
{
public static Stopwatch stopWatch = new Stopwatch();
public static TimeSpan span = new TimeSpan();
public static double totalTime = 0.0;
public static int iterations = 1000000;
static void Main(string[] args)
{
int size = 1000;
int startIndex = 0;
int endIndex = 99;
byte[] array1 = new byte[size];
byte[] array2 = new byte[endIndex - startIndex + 1];
for (int index = startIndex; index < size ; index++)
{
array1[index] = (byte)index;
}
ArraySegment<byte> arraySeg;
for (int index = 0; index < iterations; index++)
{
stopWatch.Start();
arraySeg = new ArraySegment<byte>(array1, startIndex, endIndex);
stopWatch.Stop();
totalTime += stopWatch.Elapsed.TotalMilliseconds;
}
Console.WriteLine("ArraySegment:{0:F6}", totalTime / iterations);
stopWatch.Reset();
totalTime = 0.0;
for (int index = 0; index < iterations; index++)
{
stopWatch.Start();
Array.Copy(array1, startIndex, array2, 0, endIndex);
stopWatch.Stop();
totalTime += stopWatch.Elapsed.TotalMilliseconds;
}
Console.WriteLine("Array.Copy:{0:F6}", totalTime / iterations);
}
}
}