Skip to content

Commit e193680

Browse files
committed
ReadByte or Read
1 parent 85c4949 commit e193680

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/readbyte/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Reading each byte of a buffer
2+
3+
Each byte (`bg`) can be read using:
4+
- `_, _ = bb.Read(bg)`
5+
- `bg, _ = bb.ReadByte()`
6+
7+
So, if you're treating everybyte, a 30% benefit to use ReadByte.
8+
One less variable is part of the explanation.
9+
10+
`src>go test -bench=. ./returnbyte`
11+
12+
**Results**
13+
14+
```
15+
goos: windows
16+
goarch: amd64
17+
pkg: github.com/iWdGo/GoCompilerEfficiency/src/readbyte
18+
BenchmarkBufferRead-4 200000 8540 ns/op
19+
BenchmarkRBufferReadByte-4 200000 5971 ns/op
20+
PASS
21+
```
22+
23+

src/readbyte/readbyte.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package readbyte
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"log"
8+
"strconv"
9+
)
10+
11+
const (
12+
filling = "Filling the buffer "
13+
times = 20
14+
want = len(filling)*times + 9 + 2*(times-9) // 9 one digit + 11 2-digits (11 to 19)
15+
)
16+
17+
func fillBuffer(f int) *bytes.Buffer {
18+
b := new(bytes.Buffer)
19+
for i := 1; i <= f; i++ {
20+
if c, err := fmt.Fprintf(b, "%s%d", filling, i); err != nil {
21+
fmt.Println("filling buffer failed")
22+
} else if c != len(filling)+len(strconv.Itoa(i)) {
23+
fmt.Printf("incomplete sentence written (*bytes.Buffer): got %d, want %d\n", c, len(filling))
24+
}
25+
}
26+
if b.Len() != want {
27+
log.Fatalf("filling buffer failed: got %d, want %d\n", b.Len(), want)
28+
}
29+
return b
30+
}
31+
32+
func ReadAllBytesRead(b *bytes.Buffer) (l int) {
33+
var err error
34+
var n int // avoid shadowing
35+
gotb := make([]byte, 1)
36+
for err != io.EOF {
37+
n, err = b.Read(gotb)
38+
l += n
39+
}
40+
return
41+
}
42+
43+
func ReadAllBytesReadByte(b *bytes.Buffer) (l int) {
44+
var err error
45+
var gotb, e byte
46+
for err != io.EOF {
47+
gotb, err = b.ReadByte()
48+
if gotb != e {
49+
l++
50+
}
51+
}
52+
return
53+
}

src/readbyte/readbyte_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package readbyte
2+
3+
// Counting bytes until EOF
4+
import (
5+
"testing"
6+
)
7+
8+
func TestBufferRead(t *testing.T) {
9+
b := fillBuffer(times)
10+
if got := ReadAllBytesRead(b); got != want {
11+
t.Fatalf("buffer read failed: want %d, got %d\n", want, got)
12+
}
13+
}
14+
15+
func TestBufferReadByte(t *testing.T) {
16+
b := fillBuffer(times)
17+
if got := ReadAllBytesReadByte(b); got != want {
18+
t.Fatalf("buffer read failed: want %d, got %d\n", want, got)
19+
}
20+
}
21+
22+
/*
23+
func TestBufferReadDelim(t *testing.T) {
24+
}
25+
*/
26+
// w/o the for loop, no benchmarking occurs because of optimization
27+
func BenchmarkBufferRead(b *testing.B) {
28+
for n := 0; n < b.N; n++ {
29+
ReadAllBytesRead(fillBuffer(times))
30+
}
31+
}
32+
func BenchmarkRBufferReadByte(b *testing.B) {
33+
for n := 0; n < b.N; n++ {
34+
ReadAllBytesReadByte(fillBuffer(times))
35+
}
36+
}
37+
38+
/*
39+
func BenchmarkReturnBufferBytes(b *testing.B) {
40+
for n := 0; n < b.N; n++ {
41+
ReturnBufferBytes(times)
42+
}
43+
}
44+
*/

0 commit comments

Comments
 (0)