fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞
来源:dev.to
时间:2025-02-18 14:06:42 368浏览 收藏
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞》,聊聊,我们一起来看看吧!
在go编程的世界中,fmt.sprintf函数通常是首选,因为它易于语法和格式化不同的数据类型的灵活性。但是,这种宽松的价格 - 额外的cpu开销和>内存分配并不总是理想的,尤其代码。
>本文讨论了为什么有时会“在您的口袋里燃烧一个洞”,哪些替代方案以及这些选择更好。另外,我们还包括一些基准来显示性能差异。>
tl; dr>本文探讨了在go中进行字符串串联和转换的各种方法。它表明,对于简单的情况,使用 运算符的直接串联是最快的,而strings.builder and strings.join更适合更适合更复杂或迭代的场景,这是由于> 较低的内存开销。此外,对于将整数之类的值转换为 floats>和> booleans的值,使用strconv package
,远比fmt.sprintf要高得多。基准测试结果返回了这些建议,显示了不同方法的速度和内存使用差异显着差异。
为什么fmt.sprintf效率低下?
- >即使fmt.sprintf易于使用,您也需要牢记一些性能方面:
- >解析格式开销:fmt.sprintf的每个呼叫都要求解析格式字符串以查找占位符。此过程添加了额外的cpu load 。
- > 键入转换和反射用法:由于参数以接口{}的形式传递,因此该函数必须进行键入转换,有时会使用反射 ,使其比更具体的方法慢。
- 内存分配:动态格式化过程通常需要额外的内存分配。当反复调用(如循环中)时,这些小型分配会累加,受伤的性能 。
组合字符串的替代解决方案
有几种替代方案可以帮助减少fmt.sprintf的开销:
1。与 运算符的直接串联
结合字符串的最简单方法是使用 运算符。例如:
import "strconv" value := 123 result := "value: " + strconv.itoa(value)
当更好的时候:
- 对于>简单操作在循环外部或串联数据量不大时 当代码清晰度为优先事项并且性能并不重要时。
- 优点:
>清洁和
- 易于阅读的语法。
- go编译器可以优化简单的串联。
- 缺点:
在大循环中不有效
- ,因为它会创建许多新的字符串和重复的内存分配。
- > 示例用法:
func stringconcatenation(a, b string, i int) string { return a + b + strconv.itoa(i) } func stringbuilder(a, b string, i int) string { var sb strings.builder sb.writestring(a) sb.writestring(b) sb.writestring(strconv.itoa(i)) return sb.string() } func fmtsprintf(a, b string, i int) string { return fmt.sprintf("%s%s%d", a, b, i) } func stringsjoin(a, b string, i int) string { return strings.join([]string{a, b, strconv.itoa(i)}, "") }基准结果:
benchmarkstringconcatenation-20 46120149 27.43 ns/op 7 b/op 0 allocs/op
benchmarkstringbuilder-20 17572586 93.52 ns/op 62 b/op 3 allocs/op
benchmarkfmtsprintf-20 9388428 128.20 ns/op 63 b/op 4 allocs/op
benchmarkstringsjoin-20 28760307 70.22 ns/op 31 b/op 1 allocs/op
与 的直接串联表现最佳,具有最快的执行时间(27.43 ns/op)和>
无额外的内存分配(0 allocs/op,7 b/op)。相反,fmt.sprintf最慢(128.20 ns/op),大多数内存使用情况(4个allocs/op,63 b/op)。 strings.join比fmt.sprintf(70.22 ns/op,1 allocs/op,31 b/op)更快。 2。使用字符串
strings.builder软件包是通过减少重复的内存分配来更有效地构建字符串的。
import ( "strconv" "strings" ) value := 123 var sb strings.builder sb.writestring("value: ") sb.writestring(strconv.itoa(value)) result := sb.string()
当更好的时候:
非常适合环路或需要组合许多弦乐件时。
降低内存分配的数量时,
- 。
- >。
- 优点:
。
> 在重复弦构建方案中,- 比直接串联的速度快。
- > 缺点:
- 比使用 ocerator的详细内容。
可能过于杀伤。
>- slice的示例:
var ( words [][]string = [][]string{ {"hello", "world", "apple", "canon", "table"}, {"table", "apple", "world", "hello", "canon"}, {"canon", "world", "table", "apple", "hello"}, {"apple", "canon", "hello", "world", "table"}, {"world", "table", "canon", "hello", "apple"}, {"hello", "apple", "world", "canon", "table"}, } ) func stringconcatenationwithwords(a, b string, i int) string { result := a + b + strconv.itoa(i) for _, word := range words[i] { result += word } return result } func stringbuilderwithwords(a, b string, i int) string { var sb strings.builder sb.writestring(a) sb.writestring(b) sb.writestring(strconv.itoa(i)) for _, word := range words[i] { sb.writestring(word) } return sb.string() } func fmtsprintfwithwords(a, b string, i int) string { result := fmt.sprintf("%s%s%d", a, b, i) for _, word := range words[i] { result += word } return result } func stringsjoinwithwords(a, b string, i int) string { slice := []string{a, b, strconv.itoa(i)} slice = append(slice, words[i]...) return strings.join(slice, "") }
benchmarkstringconcatenationwithwords-20 3029992 363.5 ns/op 213 b/op 6 allocs/op benchmarkstringbuilderwithwords-20 6294296 189.8 ns/op 128 b/op 4 allocs/op benchmarkfmtsprintfwithwords-20 2228869 472.1 ns/op 244 b/op 9 allocs/op benchmarkstringsjoinwithwords-20 3835489 264.4 ns/op 183 b/op 2 allocs/op基于数据,strings.builder在字符串串联中脱颖而出,提供最快的执行时间(189.8 ns/op)和
fmt.sprintf执行最差(472.1 ns/op,9 allocs/op,244 b/op),而strings.jon.join优于fmt.sprintf,但效率仍然低于弦乐。
转换为字符串的替代解决方案除了结合字符串外,还有更有效的方法将值转换为字符串而不使用fmt.sprintf。对于简单的转换,strconv软件包提供了更快且使用更少内存的专用功能。例如,要将整数转换为字符串,您可以使用strconv.itoa:
import "strconv" func convertinttostring(i int) string { return strconv.itoa(i) }对于其他数据类型,也有类似的功能:
float
:使用strconv.formatfloat将浮子转换为字符串。您可以根据需要调整格式,精度和位大小。import "strconv" func convertfloattostring(f float64) string { // 'f' is the format, -1 for automatic precision, and 64 for float64 return strconv.formatfloat(f, 'f', -1, 64) }
bool:使用strconv.formatbool将布尔值转换为字符串。
import "strconv" func convertbooltostring(b bool) string { return strconv.formatbool(b) }
- int64和uint64 :对于较大的整数类型,请使用strconv.formatint和strconv.formatuint。
import "strconv" func convertint64tostring(i int64) string { return strconv.formatint(i, 10) // base 10 for decimal } func convertuint64tostring(u uint64) string { return strconv.formatuint(u, 10) }
- 更好的性能:strconv函数是专门用于类型转换的,因此它们避免了fmt.sprintf中的格式解析的额外开销。
- >内存效率:它们通常进行较少的内存分配,因为它们执行直接转换而无需复杂的格式。
清晰而特定的代码:每个功能都有特定的目的,使您的代码更易于阅读和理解。>
例如,以下是一些简单的基准测试,用于比较strconv和fmt.sprintf的各种类型:
func benchmarkconvertinttostring(b *testing.b) { for i := 0; i < b.n; i++ { _ = strconv.itoa(12345) } } func benchmarkfmtsprintfint(b *testing.b) { for i := 0; i < b.n; i++ { _ = fmt.sprintf("%d", 12345) } } func benchmarkconvertfloattostring(b *testing.b) { for i := 0; i < b.n; i++ { _ = strconv.formatfloat(12345.6789, 'f', 2, 64) } } func benchmarkfmtsprintffloat(b *testing.b) { for i := 0; i < b.n; i++ { _ = fmt.sprintf("%f", 12345.6789) } } func benchmarkconvertbooltostring(b *testing.b) { for i := 0; i < b.n; i++ { _ = strconv.formatbool(true) } } func benchmarkfmtbooltostring(b *testing.b) { for i := 0; i < b.n; i++ { _ = fmt.sprintf("%t", true) } } func benchmarkconvertuinttostring(b *testing.b) { for i := 0; i < b.n; i++ { _ = strconv.formatuint(12345, 10) } } func benchmarkfmtsprintfuint(b *testing.b) { for i := 0; i < b.n; i++ { _ = fmt.sprintf("%d", 12345) } }
和结果:
BenchmarkConvertIntToString-20 67305488 18.15 ns/op 7 B/op 0 allocs/op BenchmarkFmtSprintfInt-20 22410037 51.15 ns/op 16 B/op 2 allocs/op BenchmarkConvertFloatToString-20 16426672 69.97 ns/op 24 B/op 1 allocs/op BenchmarkFmtSprintfFloat-20 10099478 114.1 ns/op 23 B/op 2 allocs/op BenchmarkConvertBoolToString-20 1000000000 0.1047 ns/op 0 B/op 0 allocs/op BenchmarkFmtBoolToString-20 37771470 30.62 ns/op 4 B/op 1 allocs/op BenchmarkConvertUintToString-20 84657362 18.29 ns/op 7 B/op 0 allocs/op BenchmarkFmtSprintfUint-20 25607198 49.00 ns/op 16 B/op 2 allocs/op这些基准测试表明,strconv提供更快的执行,并且比fmt.sprintf使用少的内存来将值转换为字符串。因此,对于基本转换(例如
在本文中,我们仔细研究了从fmt.sprintf中组合和转换字符串的各种方法,以直接与 ocerator,strings.builder和strings.join直接串联。基准测试表明,对于简单的串联
, 运算符效果最佳,而strings.builder and strings.join则最适合more
int
,
, bool)要比使用fmt.sprintf。 >我们希望这篇文章可以很好地了解如何在go中优化字符串处理。随时发表评论或分享您的经验。让我们一起协作和改进我们的go代码!>
好了,本文到此结束,带大家了解了《fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
-
505 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
110 收藏
-
483 收藏
-
450 收藏
-
172 收藏
-
467 收藏
-
191 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习