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
17 changes: 15 additions & 2 deletions wordcount/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ 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 this branch with buffering:
`"shakespeare.txt": 741200 words, duration: 97406ms`


# 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.
12 changes: 11 additions & 1 deletion wordcount/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"bufio"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
"time"
"unicode"
)
Expand All @@ -21,11 +23,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