Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created August 25, 2019 11:21
Show Gist options
  • Save vlad-bezden/e607864aa498d7ae415f1eecc7257a10 to your computer and use it in GitHub Desktop.
Save vlad-bezden/e607864aa498d7ae415f1eecc7257a10 to your computer and use it in GitHub Desktop.
Example of reverse string in Go using strings.Builder. This function about 3 times faster than using string concatenation
//Reverse reverses string using strings.Builder. It's about 3 times faster
//than the one with using a string
// BenchmarkReverse-8 3000000 499 ns/op 56 B/op 6 allocs/op
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()
}
//Reverse reverses string using string
// BenchmarkReverse-8 1000000 1571 ns/op 176 B/op 29 allocs/op
// func Reverse(in string) (out string) {
// for _, r := range in {
// out = string(r) + out
// }
// return
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment