Skip to content

Instantly share code, notes, and snippets.

@ilyabrin
Last active December 28, 2017 22:31
Show Gist options
  • Save ilyabrin/d65998e7cdd1cb6ffb4aecae1c8a42b5 to your computer and use it in GitHub Desktop.
Save ilyabrin/d65998e7cdd1cb6ffb4aecae1c8a42b5 to your computer and use it in GitHub Desktop.
Acronym benchmarks
package acronym
import (
"strings"
"unicode"
)
// Abbreviate e.g. Ruby on Rails as ROR
// http://exercism.io/submissions/04669e67e3c041a398a198db0bc503df
func Abbreviate(s string) (out string) {
s = strings.Replace(s, "-", " ", -1)
words := strings.Fields(s)
for i := range words {
out += string(words[i][0])
}
return strings.ToUpper(out)
}
// with dash in for loop
// comment from RobertBeilich
func AbbreviateWithDash(s string) (out string) {
s = strings.Replace(s, "-", " ", -1)
words := strings.Fields(s)
for _, word := range words {
out += string(word[0])
}
return strings.ToUpper(out)
}
// http://exercism.io/submissions/3851b757e0854c45bd71199397ead14e
func AbbreviateUnicode(s string) (result string) {
// Replace all punctuation with spaces, and then split the result into words (AKA fields).
words := strings.Fields(
strings.Map(func(r rune) rune {
if unicode.IsPunct(r) {
return rune(' ')
}
return r
}, s))
for _, word := range words {
result += string([]rune(word)[0])
}
return strings.ToUpper(result)
}
// on my Macbook Pro results are:
//
// go test -v -bench . -benchmem
//
// goos: darwin
// goarch: amd64
// BenchmarkAcronym-2 500000 4888 ns/op 600 B/op 48 allocs/op
// BenchmarkWithDash-2 500000 4741 ns/op 600 B/op 48 allocs/op
// BenchmarkAcronymUnicode-2 500000 7527 ns/op 728 B/op 52 allocs/op
package acronym
import "testing"
func TestAcronym(t *testing.T) {
for _, test := range stringTestCases {
actual := Abbreviate(test.input)
if actual != test.expected {
t.Errorf("Acronym test [%s], expected [%s], actual [%s]", test.input, test.expected, actual)
}
}
}
func TestAcronymWithDash(t *testing.T) {
for _, test := range stringTestCases {
actual := AbbreviateWithDash(test.input)
if actual != test.expected {
t.Errorf("Acronym test [%s], expected [%s], actual [%s]", test.input, test.expected, actual)
}
}
}
func TestAcronymUnicode(t *testing.T) {
for _, test := range stringTestCases {
actual := AbbreviateUnicode(test.input)
if actual != test.expected {
t.Errorf("Acronym test [%s], expected [%s], actual [%s]", test.input, test.expected, actual)
}
}
}
func BenchmarkAcronym(b *testing.B) {
b.N = 500000
for _, tc := range stringTestCases {
for i := 0; i < b.N; i++ {
Abbreviate(tc.input)
}
}
}
func BenchmarkWithDash(b *testing.B) {
b.N = 500000
for _, tc := range stringTestCases {
for i := 0; i < b.N; i++ {
AbbreviateWithDash(tc.input)
}
}
}
func BenchmarkAcronymUnicode(b *testing.B) {
b.N = 500000
for _, tc := range stringTestCases {
for i := 0; i < b.N; i++ {
AbbreviateUnicode(tc.input)
}
}
}
package acronym
// Source: exercism/problem-specifications
// Commit: cae7ae1 acronym: remove test case with mixed-case phrase (#788)
// Problem Specifications Version: 1.1.0
type acronymTest struct {
input string
expected string
}
var stringTestCases = []acronymTest{
{
input: "Portable Network Graphics",
expected: "PNG",
},
{
input: "Ruby on Rails",
expected: "ROR",
},
{
input: "First In, First Out",
expected: "FIFO",
},
{
input: "PHP: Hypertext Preprocessor",
expected: "PHP",
},
{
input: "GNU Image Manipulation Program",
expected: "GIMP",
},
{
input: "Complementary metal-oxide semiconductor",
expected: "CMOS",
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment