gpt4 book ai didi

go - 在 golang 中使用 for 循环反转字符串效率低吗?

转载 作者:数据小太阳 更新时间:2023-10-29 03:30:47 25 4
gpt4 key购买 nike

我在 Golang 中是这样做的:

func reverseStr(str string) string {
var reversed string

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

return reversed
}

我是初学者,目前不能做得更好,但我仍在学习中。我想知道我的方法是否比我在网上看到的使用 rune 的方法效率低:

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

最佳答案

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

与 rune 或 for 循环无关。您的方法一遍又一遍地构建、重建和重建字符串。而另一个通过简单地交换字符就地反转字符串。而且随着字符串变大,差异只会越来越大。

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.")
}
}

结果

$ 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

关于go - 在 golang 中使用 for 循环反转字符串效率低吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56466806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com