io.Copy和io.CopyBuffer之间有什么区别?

huangapple go评论71阅读模式
英文:

What's the difference between io.Copy and io.CopyBuffer?

问题

io.CopyBuffer的文档中提到:

> CopyBuffer与Copy相同,只是它会通过提供的缓冲区(如果需要)进行分阶段操作,而不是分配一个临时缓冲区。

这是否意味着io.CopyBuffer会先将数据复制到缓冲区,然后再复制到目标位置,从而减少对源Write的调用次数?

英文:

In the documentation of io.CopyBuffer it states:

> CopyBuffer is identical to Copy except that it stages through the
> provided buffer (if one is required) rather than allocating a
> temporary one.

Does that mean that io.CopyBuffer will copy first to buffer and then into destination making less calls to source Write?

答案1

得分: 2

CopyBuffer允许您分配自己的缓冲区。它与Copy函数的功能相同。如果您查看Copy函数,您会发现它只是调用CopyBuffer函数。

CopyBuffer允许您选择自己的缓冲区大小。io.Copy函数默认使用32K的缓冲区。如果您知道要复制的内容很大,使用更大的缓冲区可能会提高性能。除了允许调用者控制缓冲区的大小外,调用者还可以将单个缓冲区用于多个复制操作。

英文:

CopyBuffer let’s you allocate your own buffer. It is otherwise the same as
Copy. If you look at Copy, it just calls CopyBuffer.

CopyBuffer let's you choose your own buffer size. io.Copy by default uses a 32K
buffer. If you know your copy will be large, a larger buffer may be more
performant. In addition to allowing the caller to control the size of the
buffer, a caller can use a single buffer for multiple copy operations.

huangapple
  • 本文由 发表于 2022年10月1日 23:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/73919671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定