Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Jan 7, 2024
1 parent d71ce36 commit 02bde0f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ goos: darwin
goarch: arm64
pkg: resenje.org/casbab
BenchmarkCamel
BenchmarkCamel-10 1826664 642.2 ns/op 640 B/op 9 allocs/op
BenchmarkCamel-10 2062256 567.3 ns/op 640 B/op 9 allocs/op
BenchmarkPascal
BenchmarkPascal-10 1830391 638.6 ns/op 640 B/op 9 allocs/op
BenchmarkPascal-10 2110584 569.3 ns/op 640 B/op 9 allocs/op
BenchmarkSnake
BenchmarkSnake-10 1868508 653.8 ns/op 640 B/op 9 allocs/op
BenchmarkSnake-10 2087415 569.7 ns/op 640 B/op 9 allocs/op
BenchmarkCamelSnake
BenchmarkCamelSnake-10 1782997 685.2 ns/op 640 B/op 9 allocs/op
BenchmarkCamelSnake-10 2055385 586.4 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingSnake
BenchmarkScreamingSnake-10 1583528 740.1 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingSnake-10 1880961 636.2 ns/op 640 B/op 9 allocs/op
BenchmarkKebab
BenchmarkKebab-10 1859630 640.6 ns/op 640 B/op 9 allocs/op
BenchmarkKebab-10 2104927 572.2 ns/op 640 B/op 9 allocs/op
BenchmarkCamelKebab
BenchmarkCamelKebab-10 1825042 658.5 ns/op 640 B/op 9 allocs/op
BenchmarkCamelKebab-10 2051250 583.5 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingKebab
BenchmarkScreamingKebab-10 1685986 713.3 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingKebab-10 1887795 639.3 ns/op 640 B/op 9 allocs/op
BenchmarkLower
BenchmarkLower-10 1862577 640.1 ns/op 640 B/op 9 allocs/op
BenchmarkLower-10 2118400 567.7 ns/op 640 B/op 9 allocs/op
BenchmarkTitle
BenchmarkTitle-10 1818093 657.6 ns/op 640 B/op 9 allocs/op
BenchmarkTitle-10 2053699 580.1 ns/op 640 B/op 9 allocs/op
BenchmarkScreaming
BenchmarkScreaming-10 1647198 725.4 ns/op 640 B/op 9 allocs/op
BenchmarkScreaming-10 1895035 652.1 ns/op 640 B/op 9 allocs/op
PASS
```
32 changes: 23 additions & 9 deletions casbab.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,14 @@ func join(elems [][]rune) string {
}

var b strings.Builder
b.WriteString(string(elems[0]))

for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}
return b.String()
}
Expand All @@ -276,15 +281,20 @@ func joinSep(elems [][]rune, sep rune) string {
}

var b strings.Builder
b.WriteString(string(elems[0]))

for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteRune(sep)
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}
return b.String()
}

func joinWrap(elems [][]rune, sep rune, prefixSize, suffixSize int) string {
func joinWrap(elems [][]rune, sep rune, head, tail int) string {
switch len(elems) {
case 0:
return ""
Expand All @@ -294,17 +304,21 @@ func joinWrap(elems [][]rune, sep rune, prefixSize, suffixSize int) string {

var b strings.Builder

for i := 0; i < prefixSize; i++ {
for i := 0; i < head; i++ {
b.WriteRune(sep)
}

b.WriteString(string(elems[0]))
for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteRune(sep)
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}

for i := 0; i < suffixSize; i++ {
for i := 0; i < tail; i++ {
b.WriteRune(sep)
}
return b.String()
Expand Down

0 comments on commit 02bde0f

Please sign in to comment.