go - 在golang中使用for循环来反转字符串效率低吗?
问题描述
我是这样做的 Golang :
func reverseStr(str string) string {
var reversed string
for i := len(str) - 1; i >= 0; i-- {
reversed += string(str[i])
}
return reversed
}
我是初学者,现在不能做得更好,但我仍在学习。我想知道我的方法是否比我在网上看到的使用符文的方法效率低:
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)
}
解决方案
我想知道我的方法是否比我在网上看到的使用符文的方法效率低
与符文或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
推荐阅读
- javascript - 整个引导导航栏是一个png?
- microsoft-graph-api - 阅读 Planner Tasks 返回 403 和 404
- python - Matplotlib - 条形图上的虚线误差条
- multithreading - 加入 DLL_DETACH 中的线程
- php - 表格转文本文件
- assembly - MIPS 检查用户输入是否在数组中
- html - 将特定选项卡的内容显示为其子项
- uwp - ContextFlyout 和 SwipeControl 冲突
- python - 为什么程序在返回相同的答案时不接受我的代码?
- windows - 构建批处理程序来监控、创建和删除电池日志