golang函数可变参数传递性能问题
2014-06-23 12:58
537 查看
几天前纠结了一个蛋疼的问题,在go里面函数式支持可变参数的,譬如...T,go会创建一个slice,用来存放传入的可变参数,那么,如果创建一个slice,例如a,然后以a...这种方式传入,go会不会还会新建一个slice,将a的数据全部拷贝一份过去?
如果a很大,那么将会造成很严重的性能问题,不过后来想想,可能是自己多虑了,于是查看go的文档,发现如下东西:
如果a很大,那么将会造成很严重的性能问题,不过后来想想,可能是自己多虑了,于是查看go的文档,发现如下东西:
Passing arguments to ... parameters
If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T. If f is invoked with no actual arguments for p, the value passed to p is nil. Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T. The length and capacity of the slice is therefore the number of arguments bound to p and may differ for each call site.
Given the function and calls
If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.
Given the slice s and call
也就是说,如果我们传入的是slice...这种形式的参数,go不会创建新的slice。写了一个简单的例子验证:If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T. If f is invoked with no actual arguments for p, the value passed to p is nil. Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T. The length and capacity of the slice is therefore the number of arguments bound to p and may differ for each call site.
Given the function and calls
func Greeting(prefix string, who ...string) Greeting("nobody") Greeting("hello:", "Joe", "Anna", "Eileen")within Greeting, who will have the value nil in the first call, and []string{"Joe", "Anna", "Eileen"} in the second.
If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.
Given the slice s and call
s := []string{"James", "Jasmine"} Greeting("goodbye:", s...)within Greeting, who will have the same value as s with the same underlying array.
package main import "fmt" func t(args ...int) { fmt.Printf("%p\n", args) } func main() { a := []int{1,2,3} b := a[1:] t(a...) t(b...) fmt.Printf("%p\n", a) fmt.Printf("%p\n", b) } //output 0x1052e120 0x1052e124 0x1052e120 0x1052e124可以看到,可变参数args的地址跟实际外部slice的地址一样,用的同一个slice。
相关文章推荐
- golang函数可变参数传递性能问题
- C语言中的可变参数宏/函数,及可变参数在函数中的传递问题全解析
- EXT传递参数的问题 js函数的参数传递(arguments)
- 如何实现VBA中函数的可变参数传递
- 函数中的可变参数传递原理
- 有关va_list和vsnprintf输出函数的问题(可变参数)
- asp.net(C#)函数对象参数传递的问题
- 用指针传递函数参数的问题
- c语言 函数 用指针传递参数 问题
- 指针作为函数的参数传递的问题
- 函数参数的传递问题(指针的指针)(转)
- C#调用C++写的dll 函数传递参数问题,请有经验的人详细描述
- Linux与Windows下可变参数函数问题
- C++中函数参数的对象值传递问题
- 关于C#函数对象参数传递的问题
- perl 函数参数中传递数组和引用的一些问题
- Delphi里函数参数传递的问题
- 函数参数的传递 动态内存传递问题(指针的指针)
- c++之指针作为函数参数传递的问题
- 关于C#函数对象参数传递的问题