Skip to content

Commit

Permalink
improve performance of parseDVBTime (#51)
Browse files Browse the repository at this point in the history
Using time.Date() instead of time.Parse() and fmt.Sprintf() increases
performance by 600%. A benchmark has been added in order to prove it.
  • Loading branch information
aler9 authored Aug 4, 2023
1 parent 0df190a commit d345807
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dvb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func parseDVBTime(i *astikit.BytesIterator) (t time.Time, err error) {
if mt == 14 || mt == 15 {
k = 1
}
var y = yt + k
var y = 1900 + yt + k
var m = mt - 1 - k*12
t, _ = time.Parse("06-01-02", fmt.Sprintf("%d-%d-%d", y, m, d))
t = time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)

// Time
var s time.Duration
Expand Down
6 changes: 6 additions & 0 deletions dvb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ func TestWriteDVBDurationSeconds(t *testing.T) {
assert.Equal(t, n, buf.Len())
assert.Equal(t, dvbDurationSecondsBytes, buf.Bytes())
}

func BenchmarkDVBTime(b *testing.B) {
for i := 0; i < b.N; i++ {
parseDVBTime(astikit.NewBytesIterator(dvbTimeBytes))
}
}

0 comments on commit d345807

Please sign in to comment.