Skip to content

Commit

Permalink
for the lulz
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Oct 12, 2024
1 parent 8260bed commit b55b0ac
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
48 changes: 48 additions & 0 deletions internal/fasttime/benchmarks.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
= BenchmarkTs2Int

== i7-4600U
=== 32bit

[source]
---
PS C:\slackdump\internal\fasttime>go test . -bench ".*" -count=10
goos: windows
goarch: 386
pkg: github.com/rusq/slackdump/v3/internal/fasttime
cpu: Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
BenchmarkTs2Int-4 3084236 388.5 ns/op
BenchmarkTs2Int-4 3130807 379.5 ns/op
BenchmarkTs2Int-4 3134098 382.9 ns/op
BenchmarkTs2Int-4 3142426 378.4 ns/op
BenchmarkTs2Int-4 3133819 382.8 ns/op
BenchmarkTs2Int-4 3152484 376.7 ns/op
BenchmarkTs2Int-4 3076028 377.8 ns/op
BenchmarkTs2Int-4 3143527 379.2 ns/op
BenchmarkTs2Int-4 3134566 380.1 ns/op
BenchmarkTs2Int-4 3152730 378.8 ns/op
PASS
ok github.com/rusq/slackdump/v3/internal/fasttime 16.000s
---

=== 64bit

[source]
---
PS C:\slackdump\internal\fasttime> go test . -bench ".*" -count=10
goos: windows
goarch: amd64
pkg: github.com/rusq/slackdump/v3/internal/fasttime
cpu: Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
BenchmarkTs2Int-4 15718426 73.29 ns/op
BenchmarkTs2Int-4 16554610 72.74 ns/op
BenchmarkTs2Int-4 13828022 73.78 ns/op
BenchmarkTs2Int-4 14210768 72.53 ns/op
BenchmarkTs2Int-4 14328169 74.43 ns/op
BenchmarkTs2Int-4 16349811 72.58 ns/op
BenchmarkTs2Int-4 16485547 72.99 ns/op
BenchmarkTs2Int-4 16368154 72.58 ns/op
BenchmarkTs2Int-4 13619184 73.89 ns/op
BenchmarkTs2Int-4 16509716 72.28 ns/op
PASS
ok github.com/rusq/slackdump/v3/internal/fasttime 18.064s
---
2 changes: 1 addition & 1 deletion internal/fasttime/fasttime.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TS2int(ts string) (int64, error) {
if i == -1 {
return 0, fmt.Errorf("%w: %q", ErrNotATimestamp, ts)
}
val, err := strconv.ParseUint(ts[:i]+ts[i+1:], 10, 64)
val, err := atoi(ts[:i] + ts[i+1:])
return int64(val), err
}

Expand Down
9 changes: 9 additions & 0 deletions internal/fasttime/fasttime_386.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build 386

package fasttime

import "strconv"

// int size on the 32-bit systems is 32 bit (surprise), this constraints us to slower 64-bit implementation.

var atoi = func(s string) (int64, error) { return strconv.ParseInt(s, 10, 64) }
8 changes: 8 additions & 0 deletions internal/fasttime/fasttime_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fasttime

import (
"math"
"reflect"
"strconv"
"testing"
"time"
)
Expand Down Expand Up @@ -34,6 +36,12 @@ func TestTs2int(t *testing.T) {
1674255434388009,
false,
},
{
"maxint64",
args{strconv.FormatInt(math.MaxInt64/1000, 10) + ".000"},
math.MaxInt64 / 1000 * 1000,
false,
},
{
"no dot",
args{"1674255434"},
Expand Down
9 changes: 9 additions & 0 deletions internal/fasttime/fasttime_x64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !386

package fasttime

import "strconv"

// As int on 64-bit systems is 64 bit, it is possible to use faster Atoi.

var atoi = strconv.Atoi

0 comments on commit b55b0ac

Please sign in to comment.