mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 03:11:01 -06:00
Util: Improve performance of strings.SplitString (#49115)
Replaces the regexp with calls to strings.ReplaceAll and strings.Fields for simplicity and improved performance. Signed-off-by: Dave Henderson <dave.henderson@grafana.com>
This commit is contained in:
parent
3ecee06630
commit
1f85101787
@ -3,7 +3,6 @@ package util
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@ -34,7 +33,7 @@ func SplitString(str string) []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return regexp.MustCompile("[, ]+").Split(str, -1)
|
||||
return strings.Fields(strings.ReplaceAll(str, ",", " "))
|
||||
}
|
||||
|
||||
// GetAgeString returns a string representing certain time from years to minutes.
|
||||
|
@ -58,6 +58,53 @@ func TestSplitString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSplitString(b *testing.B) {
|
||||
b.Run("empty input", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("")
|
||||
}
|
||||
})
|
||||
b.Run("single string", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test")
|
||||
}
|
||||
})
|
||||
b.Run("space-separated", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test1 test2 test3")
|
||||
}
|
||||
})
|
||||
b.Run("comma-separated", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test1,test2,test3")
|
||||
}
|
||||
})
|
||||
b.Run("comma-separated with spaces", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test1 , test2 test3")
|
||||
}
|
||||
})
|
||||
b.Run("mixed commas and spaces", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test1 , test2 test3,test4")
|
||||
}
|
||||
})
|
||||
b.Run("very long mixed", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SplitString("test1 , test2 test3,test4, test5 test6 test7,test8 test9 test10" +
|
||||
" test11 test12 test13,test14 test15 test16,test17 test18 test19,test20 test21 test22" +
|
||||
" test23,test24 test25 test26,test27 test28 test29,test30 test31 test32" +
|
||||
" test33,test34 test35 test36,test37 test38 test39,test40 test41 test42" +
|
||||
" test43,test44 test45 test46,test47 test48 test49,test50 test51 test52" +
|
||||
" test53,test54 test55 test56,test57 test58 test59,test60 test61 test62" +
|
||||
" test63,test64 test65 test66,test67 test68 test69,test70 test71 test72" +
|
||||
" test73,test74 test75 test76,test77 test78 test79,test80 test81 test82" +
|
||||
" test83,test84 test85 test86,test87 test88 test89,test90 test91 test92" +
|
||||
" test93,test94 test95 test96,test97 test98 test99,test100 ")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDateAge(t *testing.T) {
|
||||
assert.Equal(t, "?", GetAgeString(time.Time{})) // base case
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user