golang函数可变参数传递性能问题
几天前纠结了一个蛋疼的问题,在go里面函数式支持可变参数的,譬如...T,go会创建一个slice,用来存放传入的可变参数,那么,如果创建一个slice,例如a,然后以a...这种方式传入,go会不会还会新建一个slice,将a的数据全部拷贝一份过去?
如果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
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.
也就是说,如果我们传入的是slice...这种形式的参数,go不会创建新的slice。写了一个简单的例子验证:
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函数可变参数传递性能问题的更多相关文章
- ARTS-S golang函数作为参数传递
函数作为参数传递在单元测试的时候非常有用,看下面的例子. package main import "fmt" func output(f func(string, string, ...
- Python学习笔记7-把函数当参数传递、指定可变参数
把函数当参数传递 # 函数参数传递 # 面向对象编程就是把对象传来传去 # 面向函数编程就是把函数传来传去 def mytest(num): return num * 2 # # 不光可以传递变量,还 ...
- Python中函数的参数传递与可变长参数
转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...
- GO开发[四]:golang函数
函数 1.声明语法:func 函数名 (参数列表) [(返回值列表)] {} 2.golang函数特点: a. 不支持重载,一个包不能有两个名字一样的函数 b. 函数是一等公民,函数也是一种类型,一个 ...
- 了解golang的可变参数(... parameters),这一篇就够了
在实际开发中,总有一些函数的参数个数是在编码过程中无法确定的,比如我们最常用的fmt.Printf和fmt.Println: fmt.Printf("一共有%v行%v列\n", r ...
- golang函数学习笔记
golang函数特点: a.不支持重载,一个包不能有两个名字一样的函数 b.函数是一等公民,函数也是一种类型,一个函数可以赋值给变量 c.匿名函数 d.多返回值 例子1 func add(a, b ...
- PHP函数可变参数列表的具体实现方法介绍
PHP函数可变参数列表可以通过_get_args().func_num_args().func_get_arg()这三个函数来实现.我们下面就对此做了详细的介绍. AD:2014WOT全球软件技术峰会 ...
- python:函数的参数传递方法演示
""" 函数的参数传递方法演示 """ #1.位置传参 def normal(a, b, c): print("1.位置传参:&q ...
- Golang函数-匿名函数与闭包函数
Golang函数-匿名函数与闭包函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
随机推荐
- Python 中的 if __name__ == '__main__' 该如何理解
__name__ 表示当前模块名, __main__ 表示正在运行的模块名. if __name__ == '__main__' 这句话的意思就是,当模块被直接运行时,以下代码块将被运行,当模块是被导 ...
- Linux学习之CentOS(十九)------linux 下压缩与解压之 tar、gzip、bzip2、zip、rar
将文件存储到归档文件中或者从归档文件中获取原始文件,以及为文件创建归档文件 tar [option] [modifiers] [file-list] 参数 file-list是tar进行归档和提取的文 ...
- python2.7入门---操作mysql数据库增删改查
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口.Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: G ...
- Oracle中时间和日期函数总结
查看当前日期格式:select * from nls_session_parameters where parameter='NLS_DATE_FORMAT'; 修改日期的格式: alter sess ...
- window环境搭建zookeeper,kafka集群
为了演示集群的效果,这里准备一台虚拟机(window 7),在虚拟机中搭建了单IP多节点的zookeeper集群(多IP节点的也是同理的),并且在本机(win 7)和虚拟机中都安装了kafka. 前期 ...
- sublime snippet 示例
<snippet> <content><![CDATA[local ${1:M} = {} function ${1:M}.new(cls, self) self = s ...
- nginx 日志分析工具goaccess
参考:https://www.goaccess.io/download 安装 $ wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz $ tar -xz ...
- Windows下Java调用BAT批处理不弹出cmd窗口
常规Windows下Java调用BAT方式肯定会弹出cmd窗口 Runtime.getRuntime().exec("cmd.exe /C start D:\\test.bat") ...
- Swift中如何化简标准库中冗长的类实例初始化代码
可能有些童鞋并不知道,在Swift中缩写点符号对于任何类型的任何static成员都有效. 我们实际写一个例子看一下: import UIKit class CFoo{ static let share ...
- 好用的SQLAlchemy
准备 安装SQLAlchemy框架 测试代码 知识点剖析 引入库支持 基类和引擎 实体类 声明类 数据库自动完成 CRUD 总结 这里简单的记录一下本人第一次使用SQLAlchemy这个ORM框架的过 ...