compare: Add a string sort benchmark
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 11 Apr 2022 09:27:07 +0000 (11:27 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 11 Apr 2022 09:33:58 +0000 (11:33 +0200)
compare/compare_strings_test.go

index 9c4aa58686ea34e725489c7778f2b7ca4aa39561..a73091fc65a2ce4d496d12f13b9532f69f223f36 100644 (file)
@@ -62,3 +62,22 @@ func TestLexicographicSort(t *testing.T) {
 
        c.Assert(s, qt.DeepEquals, []string{"A", "b", "Ba", "ba", "ba", "Bz"})
 }
+
+func BenchmarkStringSort(b *testing.B) {
+       prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"}
+       b.Run("LessStrings", func(b *testing.B) {
+               ss := make([][]string, b.N)
+               for i := 0; i < b.N; i++ {
+                       ss[i] = make([]string, len(prototype))
+                       copy(ss[i], prototype)
+               }
+               b.ResetTimer()
+               for i := 0; i < b.N; i++ {
+                       sss := ss[i]
+                       sort.Slice(sss, func(i, j int) bool {
+                               return LessStrings(sss[i], sss[j])
+                       })
+               }
+       })
+
+}