Skip to content

Commit

Permalink
optimize memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jun 22, 2022
1 parent ab66cdd commit 8a75314
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 528 deletions.
52 changes: 17 additions & 35 deletions lib/benchmark/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
package benchmark

import (
"context"
"crypto/rand"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
"github.com/ysmood/got"
"github.com/ysmood/got/lib/lcs"
)

func BenchmarkLinesYad(b *testing.B) {
// We use the same test file as https://github.com/sergi/go-diff/tree/master/testdata

g := got.T(b)
xs := lcs.NewLines(g.ReadFile("fixtures/speedtest1.txt").String())
ys := lcs.NewLines(g.ReadFile("fixtures/speedtest2.txt").String())

b.StartTimer()

for i := 0; i < b.N; i++ {
xs.YadLCS(g.Context(), ys)
}
}

func BenchmarkLinesGoogle(b *testing.B) {
g := got.T(b)
xs := g.ReadFile("fixtures/speedtest1.txt").String()
ys := g.ReadFile("fixtures/speedtest2.txt").String()
dmp := diffmatchpatch.New()

b.StartTimer()

for i := 0; i < b.N; i++ {
dmp.DiffMain(xs, ys, true)
}
}
var x = randStr(100)
var y = randStr(100)

func BenchmarkRandomYad(b *testing.B) {
g := got.T(b)
xs := lcs.NewChars(g.ReadFile("fixtures/rand_x.txt").String())
ys := lcs.NewChars(g.ReadFile("fixtures/rand_y.txt").String())
lcs.Compress = false
c := context.Background()

xs := lcs.NewChars(x)
ys := lcs.NewChars(y)

b.StartTimer()

for i := 0; i < b.N; i++ {
xs.YadLCS(g.Context(), ys)
xs.YadLCS(c, ys)
}
}

func BenchmarkRandomGoogle(b *testing.B) {
g := got.T(b)
xs := g.ReadFile("fixtures/rand_x.txt").String()
ys := g.ReadFile("fixtures/rand_y.txt").String()
dmp := diffmatchpatch.New()

b.StartTimer()

for i := 0; i < b.N; i++ {
dmp.DiffMain(xs, ys, false)
dmp.DiffMain(x, y, false)
}
}

func randStr(n int) string {
b := make([]byte, n)
_, _ = rand.Read(b)
return string(b)
}
1 change: 0 additions & 1 deletion lib/benchmark/fixtures/rand_x.txt

This file was deleted.

1 change: 0 additions & 1 deletion lib/benchmark/fixtures/rand_y.txt

This file was deleted.

230 changes: 0 additions & 230 deletions lib/benchmark/fixtures/speedtest1.txt

This file was deleted.

Loading

0 comments on commit 8a75314

Please sign in to comment.