求问内建copy函数和reflect.Copy函数有什么区别?

gjialin · · 3218 次点击
``` package copy import ( "reflect" "testing" ) func BenchmarkBuiltinCopy(b *testing.B) { src := make([]int, 1000) for i := 0; i < 1000; i ++ { src[i] = i } dst := make([]int, 1000) b.ResetTimer() for i := 0; i < b.N; i++ { copy(dst, src) } } func BenchmarkReflectCopy(b *testing.B) { src := make([]int, 1000) for i := 0; i < 1000; i ++ { src[i] = i } dst := make([]int, 1000) b.ResetTimer() for i := 0; i < b.N; i++ { reflect.Copy(reflect.ValueOf(dst), reflect.ValueOf(src)) } } ``` ![image.png](https://static.studygolang.com/210421/f36bab0c99fbb20c49ca7f77f3bb36d9.png) 大概测了下,内建copy函数性能要更好一点。
#1