Skip to content

Commit c54f55a

Browse files
author
Alex Kahn
committed
Replace range with direct array access
1 parent 9a4f28d commit c54f55a

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

main.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func scanRange(id int, start int64, end int64, count *atomic.Int64, wg *sync.Wai
7979

8080
for i := range bytes {
8181
hex.Encode(encoded, bytes[i:i+1])
82-
if containsLetter(encoded) {
82+
if isLetter(encoded[0]) || isLetter(encoded[1]) {
8383
// Found an alphabetical character, move on
8484
letterDetected = true
8585
break
@@ -101,12 +101,6 @@ func scanRange(id int, start int64, end int64, count *atomic.Int64, wg *sync.Wai
101101
}
102102
}
103103

104-
func containsLetter(id []byte) bool {
105-
for _, ch := range id {
106-
if ch >= 97 && ch <= 122 {
107-
return true
108-
}
109-
}
110-
111-
return false
104+
func isLetter(ch byte) bool {
105+
return ch >= 97 && ch <= 122
112106
}

main_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
"testing"
7+
)
8+
9+
func BenchmarkScanRange(b *testing.B) {
10+
wg := sync.WaitGroup{}
11+
count := atomic.Int64{}
12+
13+
for n := 0; n < b.N; n++ {
14+
wg.Add(1)
15+
scanRange(1, int64(n), int64(n+1), &count, &wg)
16+
}
17+
}

range.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

0 commit comments

Comments
 (0)