dougeqiang1619 2019-06-05 19:25
浏览 103
已采纳

通过在golang中使用for循环来反转字符串效率低下?

I did it like this Golang:

func reverseStr(str string) string {
    var reversed string

    for i := len(str) - 1; i >= 0; i-- {
        reversed += string(str[i])
    }

    return reversed
}

I'm a beginner and can't do better for now, but I'm still learning. I'd like to know is my method less efficient than the ones I saw online that use runes:

func reverse(s string) string {
    chars := []rune(s)
    for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
        chars[i], chars[j] = chars[j], chars[i]
    }
    return string(chars)
}
  • 写回答

2条回答 默认 最新

  • duangang4001 2019-06-05 20:05
    关注

    I'd like to know is my method less efficient than the ones I saw online that use runes

    Nothing to do with runes or for loop. Your method builds and rebuilds and rebuilds a string, over and over. Whereas the other one reverses the string in-place, by simply swapping characters. And the difference is only getting worse with larger strings.

    package main
    
    import "testing"
    
    func reverseConcat(str string) string {
        var reversed string
    
        for i := len(str) - 1; i >= 0; i-- {
            reversed += string(str[i])
        }
    
        return reversed
    }
    
    func reverseSwapRunes(s string) string {
        chars := []rune(s)
        for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
            chars[i], chars[j] = chars[j], chars[i]
        }
        return string(chars)
    }
    
    func BenchmarkConcatSmall(b *testing.B) {
        for i := 0; i < b.N; i++ {
            reverseConcat("hello world")
        }
    }
    
    func BenchmarkSwapRunesSmall(b *testing.B) {
        for i := 0; i < b.N; i++ {
            reverseSwapRunes("hello world")
        }
    }
    
    func BenchmarkConcatLarger(b *testing.B) {
        for i := 0; i < b.N; i++ {
            reverseConcat("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
        }
    }
    
    func BenchmarkSwapRunesLarger(b *testing.B) {
        for i := 0; i < b.N; i++ {
            reverseSwapRunes("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
        }
    }
    

    Results

    $ go test -bench . -benchmem
    goos: linux
    goarch: amd64
    BenchmarkConcatSmall-8           5000000           329 ns/op          80 B/op         10 allocs/op
    BenchmarkSwapRunesSmall-8       20000000           117 ns/op          16 B/op          1 allocs/op
    BenchmarkConcatLarger-8            30000         44877 ns/op      172833 B/op        573 allocs/op
    BenchmarkSwapRunesLarger-8        300000          5353 ns/op        2944 B/op          2 allocs/op
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了