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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料