基于Go Int转string几种方式性能测试


Posted in Golang onApril 28, 2021

Go语言内置int转string至少有3种方式:

fmt.Sprintf("%d",n)
strconv.Itoa(n)
strconv.FormatInt(n,10)

下面针对这3中方式的性能做一下简单的测试:

package gotest
import (
	"fmt"
	"strconv"
	"testing"
)
func BenchmarkSprintf(b *testing.B) {
	n := 10
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		fmt.Sprintf("%d", n)
	}
}
func BenchmarkItoa(b *testing.B) {
	n := 10
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		strconv.Itoa(n)
	}
}
func BenchmarkFormatInt(b *testing.B) {
	n := int64(10)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		strconv.FormatInt(n, 10)
	}
}

保存文件为int2string_test.go

执行:

go test -v -bench=. int2string_test.go -benchmem
goos: darwin
goarch: amd64
BenchmarkSprintf-8      20000000               114 ns/op              16 B/op          2 allocs/op
BenchmarkItoa-8         200000000                6.33 ns/op            0 B/op          0 allocs/op
BenchmarkFormatInt-8    300000000                4.10 ns/op            0 B/op          0 allocs/op
PASS
ok      command-line-arguments  5.998s

总体来说,strconv.FormatInt()效率最高,fmt.Sprintf()效率最低

补充:Golang类型转换, 整型转换成字符串,字符串转换成整型

看代码吧~

package main
 
import (
 "fmt"
 "reflect"
 "strconv"
)
 
func main() {
 //字符串转成整型int
 num,err:=strconv.Atoi("123")
 if err!=nil {
  panic(err)
 }
 fmt.Println(num,reflect.TypeOf(num))
 
 //整型转换成字符串
 str:=strconv.Itoa(123)
 fmt.Println(str,reflect.TypeOf(str))
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。如有错误或未考虑完全的地方,望不吝赐教。

Golang 相关文章推荐
为什么不建议在go项目中使用init()
Apr 12 Golang
golang通过递归遍历生成树状结构的操作
Apr 28 Golang
golang import自定义包方式
Apr 29 Golang
对Golang中的FORM相关字段理解
May 02 Golang
go select编译期的优化处理逻辑使用场景分析
Jun 28 Golang
K8s部署发布Golang应用程序的实现方法
Jul 16 Golang
golang操作redis的客户端包有多个比如redigo、go-redis
Apr 14 Golang
Golang 遍历二叉树
Apr 19 Golang
Golang 实现 WebSockets 之创建 WebSockets
Apr 24 Golang
深入理解 Golang 的字符串
May 04 Golang
GoFrame gredis缓存DoVar Conn连接对象 自动序列化GoFrame gredisDo/DoVar方法Conn连接对象自动序列化/反序列化总结
Jun 14 Golang
GoFrame框架数据校验之校验结果Error接口对象
Jun 21 Golang
Go语言中break label与goto label的区别
golang 如何用反射reflect操作结构体
Apr 28 #Golang
golang 生成对应的数据表struct定义操作
Apr 28 #Golang
golang 如何通过反射创建新对象
Apr 28 #Golang
golang 实现两个结构体复制字段
Apr 28 #Golang
go结构体嵌套的切片数组操作
Apr 28 #Golang
golang json数组拼接的实例
Apr 28 #Golang
You might like
图书管理程序(二)
2006/10/09 PHP
PHP URL路由类实例
2013/11/12 PHP
php简单定时执行任务的实现方法
2015/02/23 PHP
学习php设计模式 php实现适配器模式
2015/12/07 PHP
thinkPHP5.0框架API优化后的友好性分析
2017/03/17 PHP
php表单文件iframe异步上传实例讲解
2017/07/26 PHP
小议Function.apply()之二------利用Apply的参数数组化来提高 JavaScript程序性能
2006/11/30 Javascript
javascript(jquery)利用函数修改全局变量的代码
2009/11/02 Javascript
你必须知道的Javascript知识点之&quot;深入理解作用域链&quot;的介绍
2013/04/23 Javascript
jquery ui resize 中border-box的bug修正
2015/04/26 Javascript
jQuery定义插件的方法
2015/12/18 Javascript
js时间查询插件使用详解
2017/04/07 Javascript
node.js中express-session配置项详解
2017/05/31 Javascript
详解React-Router中Url参数改变页面不刷新的解决办法
2018/05/08 Javascript
Bootstrap标签页(Tab)插件切换echarts不显示问题的解决
2018/07/13 Javascript
BootStrap中的模态框(modal,弹出层)功能示例代码
2018/11/02 Javascript
layui-laydate时间日历控件使用方法详解
2018/11/15 Javascript
详解nuxt路由鉴权(express模板)
2018/11/21 Javascript
VUE2.0+ElementUI2.0表格el-table实现表头扩展el-tooltip
2018/11/30 Javascript
three.js搭建室内场景教程
2018/12/30 Javascript
jQuery实现轮播图效果
2019/11/26 jQuery
解决Vue使用bus总线时,第一次路由跳转时数据没成功传递问题
2020/07/28 Javascript
基于JavaScript实现简单抽奖功能代码实例
2020/10/20 Javascript
python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
2009/04/09 Python
python实现中文输出的两种方法
2015/05/09 Python
python爬取各类文档方法归类汇总
2018/03/22 Python
Python中logging.NullHandler 的使用教程
2018/11/29 Python
jupyter 导入csv文件方式
2020/04/21 Python
HTML5添加禁止缩放功能
2017/11/03 HTML / CSS
施华洛世奇德国官网:SWAROVSKI德国
2017/02/01 全球购物
世界经理人咨询有限公司面试
2014/09/23 面试题
企业文明单位申报材料
2014/05/16 职场文书
学校校庆演讲稿
2014/05/22 职场文书
小学二年级数学教学计划
2015/01/20 职场文书
泰山导游词
2015/02/02 职场文书
祝酒词范文
2015/08/12 职场文书