Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions wordcount/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ A simple timer has been used to track execution time.

Provide the name of the text file as a command line argument.

`go run main.go moby.txt`
`go run main.go shakespeare.txt`

Example file to use:
https://www.gutenberg.org/cache/epub/2701/pg2701.txt
https://www.gutenberg.org/ebooks/100


# Execution time

For base branch:
`"shakespeare.txt": 741200 words, duration: 3422897ms`

For the profile branch with buffering:
`"shakespeare.txt": 741200 words, duration: 97406ms`

For this branch with global buffer:
`"shakespeare.txt": 741200 words, duration: 67800ms`


# To view profile

`go tool pprof -http=:8080 cpuprofile.pprof`
Binary file added wordcount/cpuprofile.pprof
Binary file not shown.
Binary file added wordcount/cpuprofile01-moby.pprof
Binary file not shown.
Binary file added wordcount/cpuprofile02-shakespeare.pprof
Binary file not shown.
16 changes: 14 additions & 2 deletions wordcount/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package main

import (
"bufio"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
"time"
"unicode"
)

var buf [1]byte

func readbyte(r io.Reader) (rune, error) {
var buf [1]byte

_, err := r.Read(buf[:])
return rune(buf[0]), err
}
Expand All @@ -21,11 +25,19 @@ func main() {
log.Fatalf("could not open file %q: %v", os.Args[1], err)
}

fc, err := os.Create("cpuprofile.pprof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(fc)
defer pprof.StopCPUProfile()

start := time.Now()
words := 0
b := bufio.NewReader(f)
inword := false
for {
r, err := readbyte(f)
r, err := readbyte(b)
if err == io.EOF {
break
}
Expand Down
Loading