Skip to content

Commit 28b3135

Browse files
authored
Merge pull request #11 from timonwong/add-more-bench
Add more benchmarks
2 parents 3e6aec7 + 9d00cc2 commit 28b3135

File tree

8 files changed

+112
-19
lines changed

8 files changed

+112
-19
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ High performance C99-compatible `strftime` formatter for Go.
1313

1414
## Performance
1515

16-
Just for reference :P
16+
Comparision with the standard library `time.(*Time).Format()`:
1717

1818
```
1919
> go test -bench Bench -cpu 4 -benchmem .
@@ -27,6 +27,24 @@ PASS
2727
ok github.com/imperfectgo/go-strftime 4.245s
2828
```
2929

30+
Comparision with other libraries:
31+
32+
```
33+
> go test -bench Bench -cpu 8 -benchmem ./benchmark
34+
35+
goos: darwin
36+
goarch: amd64
37+
pkg: github.com/imperfectgo/go-strftime/benchmark
38+
BenchmarkImperfectGo-8 3000000 484 ns/op 64 B/op 1 allocs/op
39+
BenchmarkTebeka-8 300000 4161 ns/op 272 B/op 20 allocs/op
40+
BenchmarkJehiah-8 1000000 1719 ns/op 256 B/op 17 allocs/op
41+
BenchmarkFastly-8 2000000 708 ns/op 85 B/op 5 allocs/op
42+
BenchmarkLestrrat-8 1000000 1471 ns/op 240 B/op 3 allocs/op
43+
BenchmarkLestrratCachedString-8 3000000 496 ns/op 128 B/op 2 allocs/op
44+
PASS
45+
ok github.com/imperfectgo/go-strftime/benchmark 10.605s
46+
```
47+
3048
## License
3149

3250
This project can be treated as a derived work of time package from golang standard library.

benchmark_comp_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 Timon Wong. All rights reserved.
2+
3+
// +build bench
4+
5+
package strftime_test
6+
7+
import (
8+
"testing"
9+
"time"
10+
11+
fastly "github.com/fastly/go-utils/strftime"
12+
imperfectgo "github.com/imperfectgo/go-strftime"
13+
jehiah "github.com/jehiah/go-strftime"
14+
lestrrat "github.com/lestrrat-go/strftime"
15+
tebeka "github.com/tebeka/strftime"
16+
)
17+
18+
const (
19+
benchfmt = `%A %a %B %b %d %H %I %M %m %p %S %Y %y %Z`
20+
)
21+
22+
var (
23+
now = time.Now().UTC()
24+
)
25+
26+
func BenchmarkImperfectGo(b *testing.B) {
27+
for i := 0; i < b.N; i++ {
28+
imperfectgo.Format(now, benchfmt)
29+
}
30+
}
31+
32+
func BenchmarkTebeka(b *testing.B) {
33+
for i := 0; i < b.N; i++ {
34+
tebeka.Format(benchfmt, now)
35+
}
36+
}
37+
38+
func BenchmarkJehiah(b *testing.B) {
39+
// Grr, uses byte slices, and does it faster, but with more allocs
40+
for i := 0; i < b.N; i++ {
41+
jehiah.Format(benchfmt, now)
42+
}
43+
}
44+
45+
func BenchmarkFastly(b *testing.B) {
46+
for i := 0; i < b.N; i++ {
47+
fastly.Strftime(benchfmt, now)
48+
}
49+
}
50+
51+
func BenchmarkLestrrat(b *testing.B) {
52+
for i := 0; i < b.N; i++ {
53+
lestrrat.Format(benchfmt, now)
54+
}
55+
}
56+
57+
func BenchmarkLestrratCachedString(b *testing.B) {
58+
f, _ := lestrrat.New(benchfmt)
59+
// This benchmark does not take into effect the compilation time
60+
for i := 0; i < b.N; i++ {
61+
f.FormatString(now)
62+
}
63+
}

benchmark_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// Copyright 2018 Timon Wong. All rights reserved.
2-
// Copyright 2009 The Go Authors. All rights reserved.
32
// Use of this source code is governed by a BSD-style
43
// license that can be found in the LICENSE file.
54

6-
package strftime
5+
package strftime_test
76

87
import (
98
"testing"
109
"time"
10+
11+
"github.com/imperfectgo/go-strftime"
12+
)
13+
14+
var (
15+
now = time.Now().UTC()
1116
)
1217

1318
func BenchmarkStdTimeFormat(b *testing.B) {
14-
now := time.Now()
1519
for i := 0; i < b.N; i++ {
1620
now.Format(time.RFC3339Nano)
1721
}
@@ -21,6 +25,6 @@ func BenchmarkGoStrftime(b *testing.B) {
2125
const layout = "%Y-%m-%dT%H:%M:%S.%f%z"
2226
now := time.Now()
2327
for i := 0; i < b.N; i++ {
24-
Format(now, layout)
28+
strftime.Format(now, layout)
2529
}
2630
}

example_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
package strftime
1+
// Copyright 2018 Timon Wong. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package strftime_test
26

37
import (
48
"fmt"
59
"time"
10+
11+
"github.com/imperfectgo/go-strftime"
612
)
713

814
func ExampleFormat() {
915
t := time.Date(2008, 9, 3, 20, 4, 26, 654321000, time.FixedZone("CST", 8*3600))
1016

11-
fmt.Println(Format(t, "%c"))
12-
fmt.Println(Format(t, "%Y-%m-%dT%H:%M:%S.%f%z"))
13-
fmt.Println(Format(t, "%A is day number %w of the week."))
14-
fmt.Println(Format(t, "Last century was%n the %Cth century."))
15-
fmt.Println(Format(t, "Time zone: %Z"))
17+
fmt.Println(strftime.Format(t, "%c"))
18+
fmt.Println(strftime.Format(t, "%Y-%m-%dT%H:%M:%S.%f%z"))
19+
fmt.Println(strftime.Format(t, "%A is day number %w of the week."))
20+
fmt.Println(strftime.Format(t, "Last century was%n the %Cth century."))
21+
fmt.Println(strftime.Format(t, "Time zone: %Z"))
1622

1723
// Output:
1824
// Wed Sep 3 20:04:26 2008

format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
157157
}
158158
n = ((yday - n) / 7) + 1
159159
b = appendInt(b, n, 2)
160-
case stdDay:
161-
b = appendInt(b, day, 0)
160+
//case stdDay:
161+
// b = appendInt(b, day, 0)
162162
case stdUnderDay:
163163
if day < 10 {
164164
b = append(b, ' ')

format_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
// SOFTWARE.
2121

22-
package strftime
22+
package strftime_test
2323

2424
import (
2525
"testing"
2626
"time"
27+
28+
"github.com/imperfectgo/go-strftime"
2729
)
2830

2931
var (
@@ -100,7 +102,7 @@ func TestFormat(t *testing.T) {
100102

101103
for i := range tc {
102104
c := tc[i]
103-
actual := Format(c.time, c.layout)
105+
actual := strftime.Format(c.time, c.layout)
104106
if actual != c.expected {
105107
t.Errorf("Test layout `%s`: expected: %q; actual: %q", c.layout, c.expected, actual)
106108
}

stdtime_unsafe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// +build !appengine,!js
2-
31
// Copyright 2018 Timon Wong. All rights reserved.
42
// Copyright 2009 The Go Authors. All rights reserved.
53
// Use of this source code is governed by a BSD-style
64
// license that can be found in the LICENSE file.
75

6+
// +build !appengine,!js
7+
88
package strftime
99

1010
import (

stdtime_unsafe.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// +build !appengine,!js
2-
31
// Copyright 2018 Timon Wong. All rights reserved.
42
// Copyright 2009 The Go Authors. All rights reserved.
53
// Use of this source code is governed by a BSD-style
64
// license that can be found in the LICENSE file.
75

86
// Empty assembly file just make `go:linkname` work without "missing function body" error
97
// See https://github.com/golang/go/issues/15006
8+
9+
// +build !appengine,!js

0 commit comments

Comments
 (0)