0

0

fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞

碧海醫心

碧海醫心

发布时间:2025-02-15 15:46:18

|

533人浏览过

|

来源于dev.to

转载

在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)和(4个allocs/op,128 b/op)。直接串联较慢(363.5 ns/op,6个allocs/op,213 b/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

    先见AI
    先见AI

    数据为基,先见未见

    下载
    :使用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)
    }
    

    • int64uint64
    • :对于较大的整数类型,请使用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的优点:
      更好的性能: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使用少的内存来将值转换为字符串。因此,对于基本转换(例如
    • int
    • >,
    • float
    • bool
    • ),strconv是一个绝佳的选择,当不需要复杂的格式时。
    • 结论

    在本文中,我们仔细研究了从fmt.sprintf中组合和转换字符串的各种方法,以直接与 ocerator,strings.builder和strings.join直接串联。基准测试表明,对于简单的串联
    , 运算符效果最佳,而strings.builder and strings.join则最适合more 复杂或迭代方案

    。另外,使用strconv软件包进行类型转换(例如

    int

    float

    bool)要比使用fmt.sprintf。 >我们希望这篇文章可以很好地了解如何在go中优化字符串处理。随时发表评论或分享您的经验。让我们一起协作和改进我们的go代码!>

相关专题

更多
数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

301

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

222

2025.10.31

css中float用法
css中float用法

css中float属性允许元素脱离文档流并沿其父元素边缘排列,用于创建并排列、对齐文本图像、浮动菜单边栏和重叠元素。想了解更多float的相关内容,可以阅读本专题下面的文章。

558

2024.04.28

C++中int、float和double的区别
C++中int、float和double的区别

本专题整合了c++中int和double的区别,阅读专题下面的文章了解更多详细内容。

98

2025.10.23

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1463

2023.10.24

Go语言中的运算符有哪些
Go语言中的运算符有哪些

Go语言中的运算符有:1、加法运算符;2、减法运算符;3、乘法运算符;4、除法运算符;5、取余运算符;6、比较运算符;7、位运算符;8、按位与运算符;9、按位或运算符;10、按位异或运算符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

228

2024.02.23

php三元运算符用法
php三元运算符用法

本专题整合了php三元运算符相关教程,阅读专题下面的文章了解更多详细内容。

85

2025.10.17

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

254

2023.08.03

Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

0

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

PHP面向对象基础课程(更新中)
PHP面向对象基础课程(更新中)

共12课时 | 0.7万人学习

AI绘画教程
AI绘画教程

共2课时 | 0.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号