confirm that ansiCuuAndEd doesn't allocate
go test -run XXX -bench=. -benchmem
Vladimir Bauer
3 years ago
| 7 | 7 | "testing" |
| 8 | 8 | ) |
| 9 | 9 | |
| 10 | var ( | |
| 11 | out = ioutil.Discard | |
| 12 | lines = 99 | |
| 13 | ) | |
| 14 | ||
| 10 | 15 | func BenchmarkWithFprintf(b *testing.B) { |
| 11 | cuuAndEd := "\x1b[%dA\x1b[J" | |
| 16 | verb := fmt.Sprintf("%s%%d%s", escOpen, cuuAndEd) | |
| 17 | b.ResetTimer() | |
| 12 | 18 | for i := 0; i < b.N; i++ { |
| 13 | fmt.Fprintf(ioutil.Discard, cuuAndEd, 4) | |
| 19 | fmt.Fprintf(out, verb, lines) | |
| 14 | 20 | } |
| 15 | 21 | } |
| 16 | 22 | |
| 17 | 23 | func BenchmarkWithJoin(b *testing.B) { |
| 18 | bCuuAndEd := [][]byte{[]byte("\x1b["), []byte("A\x1b[J")} | |
| 24 | bCuuAndEd := [][]byte{[]byte(escOpen), []byte(cuuAndEd)} | |
| 19 | 25 | for i := 0; i < b.N; i++ { |
| 20 | _, _ = ioutil.Discard.Write(bytes.Join(bCuuAndEd, []byte(strconv.Itoa(4)))) | |
| 26 | _, _ = out.Write(bytes.Join(bCuuAndEd, []byte(strconv.Itoa(lines)))) | |
| 21 | 27 | } |
| 22 | 28 | } |
| 23 | 29 | |
| 24 | 30 | func BenchmarkWithAppend(b *testing.B) { |
| 25 | escOpen := []byte("\x1b[") | |
| 26 | cuuAndEd := []byte("A\x1b[J") | |
| 31 | escOpen := []byte(escOpen) | |
| 32 | cuuAndEd := []byte(cuuAndEd) | |
| 27 | 33 | for i := 0; i < b.N; i++ { |
| 28 | _, _ = ioutil.Discard.Write(append(strconv.AppendInt(escOpen, 4, 10), cuuAndEd...)) | |
| 34 | _, _ = out.Write(append(strconv.AppendInt(escOpen, int64(lines), 10), cuuAndEd...)) | |
| 29 | 35 | } |
| 30 | 36 | } |
| 31 | 37 | |
| 32 | func BenchmarkWithCopy(b *testing.B) { | |
| 33 | w := New(ioutil.Discard) | |
| 34 | w.lines = 4 | |
| 38 | func BenchmarkWithCurrentImpl(b *testing.B) { | |
| 35 | 39 | for i := 0; i < b.N; i++ { |
| 36 | _ = w.ansiCuuAndEd() | |
| 40 | _ = ansiCuuAndEd(out, lines) | |
| 37 | 41 | } |
| 38 | 42 | } |