iancoleman / strcase

A golang package for converting to snake_case or CamelCase
MIT License
1.01k stars 106 forks source link

Use ASCIISet to lower execution time by ~10 to 30% #48

Open elliotwutingfeng opened 1 year ago

elliotwutingfeng commented 1 year ago

Changes

Benchmarks

Command

go test -bench . -benchmem -cpu 1

Before

goos: linux
goarch: amd64
pkg: github.com/iancoleman/strcase
cpu: AMD Ryzen 7 5800X 8-Core Processor             
BenchmarkToCamel                                 2112366               575.0 ns/op           128 B/op         10 allocs/op
BenchmarkToLowerCamel                            2861402               409.6 ns/op            96 B/op          7 allocs/op
BenchmarkToSnake                                  621049              1894 ns/op             480 B/op         30 allocs/op
BenchmarkToSnakeWithIgnore                        565810              2040 ns/op             408 B/op         20 allocs/op
BenchmarkToDelimited                              850269              1374 ns/op             304 B/op         19 allocs/op
BenchmarkToScreamingSnake                       20631154                59.00 ns/op           16 B/op          1 allocs/op
BenchmarkToKebab                                19712632                60.27 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingKebab                       20477086                58.17 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingDelimited                   20021593                58.48 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingDelimitedWithIgnore          8217859               145.8 ns/op            24 B/op          1 allocs/op
PASS
ok      github.com/iancoleman/strcase   13.300s

After

goos: linux
goarch: amd64
pkg: github.com/iancoleman/strcase
cpu: AMD Ryzen 7 5800X 8-Core Processor             
BenchmarkToCamel                                 2503086               483.8 ns/op           128 B/op         10 allocs/op
BenchmarkToLowerCamel                            3385527               351.1 ns/op            96 B/op          7 allocs/op
BenchmarkToSnake                                  708350              1663 ns/op             480 B/op         30 allocs/op
BenchmarkToSnakeWithIgnore                        676756              1627 ns/op             408 B/op         20 allocs/op
BenchmarkToDelimited                              908474              1136 ns/op             304 B/op         19 allocs/op
BenchmarkToScreamingSnake                       21560983                55.12 ns/op           16 B/op          1 allocs/op
BenchmarkToKebab                                22520330                52.71 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingKebab                       22808118                51.95 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingDelimited                   21881048                51.62 ns/op           16 B/op          1 allocs/op
BenchmarkToScreamingDelimitedWithIgnore         11011074               105.1 ns/op            24 B/op          1 allocs/op
PASS
ok      github.com/iancoleman/strcase   12.786s