在go编程的世界中,fmt.sprintf函数通常是首选,因为它易于语法和格式化不同的数据类型的灵活性。但是,这种宽松的价格 - 额外的
cpu开销和>内存分配并不总是理想的,尤其代码。
>本文讨论了为什么有时会“在您的口袋里燃烧一个洞”,哪些替代方案以及这些选择更好。另外,我们还包括一些基准来显示性能差异。>
tl; dr>本文探讨了在go中进行字符串串联和转换的各种方法。它表明,对于简单的情况,使用 运算符的直接串联是最快的,而strings.builder and strings.join更适合更适合更复杂或迭代的场景,这是由于> 较低的内存开销。此外,对于将整数之类的值转换为 floats>和> booleans的值,使用strconv package
为什么fmt.sprintf效率低下?
有几种替代方案可以帮助减少fmt.sprintf的开销:
结合字符串的最简单方法是使用 运算符。例如:
import "strconv" value := 123 result := "value: " + strconv.itoa(value)
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
(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()
当更好的时候:
非常适合环路或需要组合许多弦乐件时。
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
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) }
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) }
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
在本文中,我们仔细研究了从fmt.sprintf中组合和转换字符串的各种方法,以直接与 ocerator,strings.builder和strings.join直接串联。基准测试表明,对于简单的串联
, 运算符效果最佳,而strings.builder and strings.join则最适合more
int
,
, bool)要比使用fmt.sprintf。 >我们希望这篇文章可以很好地了解如何在go中优化字符串处理。随时发表评论或分享您的经验。让我们一起协作和改进我们的go代码!>
以上就是fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号