首页 > 后端开发 > Golang > 正文

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

碧海醫心
发布: 2025-02-15 15:46:18
转载
451人浏览过

在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

    :使用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代码!>

以上就是fmtsprintf:看起来很简单,但会在口袋里燃烧一个洞的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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