问题描述
Array.Copy 和的都做同样的事情,但 BlockCopy
的目的是快速字节-Level基本数组复制,而复制
是通用的实现。我的问题是 - 在什么情况下你应该使用 BlockCopy
?如果你在当你复制原始类型的数组,或者你应该只使用它,如果你编码性能的任何时候使用它?有什么内在的危险关于使用 Buffer.BlockCopy
在 Array.Copy
?
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy
is aimed at fast byte-level primitive array copying, whereas Copy
is the general-purpose implementation. My question is - under what circumstances should you use BlockCopy
? Should you use it at any time when you are copying primitive type arrays, or should you only use it if you're coding for performance? Is there anything inherently dangerous about using Buffer.BlockCopy
over Array.Copy
?
推荐答案
由于参数Buffer.BlockCopy是字节为基础的,而不是指数型,你很可能搞砸了你的code比,如果你使用Array.Copy,所以我只会用Buffer.BlockCopy在我的code性能关键部分。
Since the parameters to Buffer.BlockCopy are byte-based rather than index-based, you're more likely to screw up your code than if you use Array.Copy, so I would only use Buffer.BlockCopy in a performance-critical section of my code.
这篇关于Array.Copy VS Buffer.BlockCopy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!