Skip to content

Commit a3a13e8

Browse files
committed
Add find number of digits example
1 parent cd38100 commit a3a13e8

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# これは何?
2+
3+
特定の数値の桁数を求める方法として
4+
5+
- 一度文字列化し、その文字数を数える
6+
- Log10の値をFloor+1する
7+
8+
という2つの方法で試した場合の速度差を見るサンプルです。
9+
10+
Gitpod上で実行すると、例えば以下のような結果が得られます。
11+
12+
```sh
13+
$ task
14+
[ToString] 9 [Log10] 9
15+
----------------------------------------
16+
=== RUN TestFn
17+
--- PASS: TestFn (0.00s)
18+
PASS
19+
ok command-line-arguments 0.002s
20+
----------------------------------------
21+
goos: linux
22+
goarch: amd64
23+
cpu: AMD EPYC 7B13
24+
BenchmarkUseToString-16 34382973 35.82 ns/op
25+
BenchmarkUseLog10-16 96657720 12.26 ns/op
26+
PASS
27+
ok command-line-arguments 2.473s
28+
```
29+
30+
後者の方が倍以上速いことが分かります。
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
tasks:
6+
default:
7+
cmds:
8+
- go run main.go
9+
- echo '----------------------------------------'
10+
- go test -v lib/*
11+
- echo '----------------------------------------'
12+
- go test -bench . lib/*
13+
silent: true
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lib
2+
3+
import (
4+
"math"
5+
"strconv"
6+
)
7+
8+
type (
9+
NumberOfDigits int
10+
)
11+
12+
func UseToString(v int) NumberOfDigits {
13+
return NumberOfDigits(len(strconv.Itoa(v)))
14+
}
15+
16+
func UseLog10(v int) NumberOfDigits {
17+
return NumberOfDigits(int(math.Floor(math.Log10(float64(v)))) + 1)
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package lib
2+
3+
import "testing"
4+
5+
func TestFn(t *testing.T) {
6+
testcases := []struct {
7+
name string
8+
in int
9+
out NumberOfDigits
10+
}{
11+
{"1", 1, NumberOfDigits(1)},
12+
{"10", 10, NumberOfDigits(2)},
13+
{"100", 100, NumberOfDigits(3)},
14+
{"9999", 9999, NumberOfDigits(4)},
15+
{"123456789", 123456789, NumberOfDigits(9)},
16+
{"1234567890", 1234567890, NumberOfDigits(10)},
17+
}
18+
19+
for _, tc := range testcases {
20+
tc := tc
21+
22+
if v := UseToString(tc.in); v != tc.out {
23+
t.Errorf("%s-UseToString: [want] %v\t[got] %v", tc.name, tc.out, v)
24+
}
25+
26+
if v := UseLog10(tc.in); v != tc.out {
27+
t.Errorf("%s-UseLog10: [want] %v\t[got] %v", tc.name, tc.out, v)
28+
}
29+
}
30+
}
31+
32+
func BenchmarkUseToString(b *testing.B) {
33+
for i := 0; i < b.N; i++ {
34+
UseToString(123456789)
35+
}
36+
}
37+
38+
func BenchmarkUseLog10(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
UseLog10(123456789)
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/devlights/try-golang/examples/singleapp/find_number_of_digits/lib"
7+
)
8+
9+
func main() {
10+
if err := run(); err != nil {
11+
panic(err)
12+
}
13+
}
14+
15+
func run() error {
16+
v := 123456789
17+
fmt.Printf("[ToString] %v\t[Log10] %v\n", lib.UseToString(v), lib.UseLog10(v))
18+
19+
return nil
20+
}

0 commit comments

Comments
 (0)