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)
}
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
Your first solution will only work with ASCII characters, because len(str) gives you len in bytes, and str[i] also return byte. As soon as you get to deal with Unicode it will fail, because one Unicode (rune) can have more than one byte.
type byte = uint8
type rune = int32
So your second solution will work with any type of string characters.
Another example is to use strings.Builder
func Reverse(in string) string {
var sb strings.Builder
runes := []rune(in)
for i := len(runes) - 1; 0 <= i; i-- {
sb.WriteRune(runes[i])
}
return sb.String()
}
which is in my tests performs almost the same as in array replacement (your second example) - 3 times faster than string concatenation.
BenchmarkReverseStrConcat-8 1000000 1571 ns/op 176 B/op 29 allocs/op
BenchmarkReverseStrBuilder-8 3000000 499 ns/op 56 B/op 6 allocs/op