forked from plusuncold/longest-word-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
46 lines (37 loc) · 856 Bytes
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// fraq <[email protected]>
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"strings"
"time"
)
func main() {
var corpus string
flag.StringVar(&corpus, "c", "corpus.txt", "Location of corpus to parse")
flag.Parse()
start := time.Now()
data, err := ioutil.ReadFile(corpus)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(strings.NewReader(string(data)))
scanner.Split(bufio.ScanWords)
max := 0
longestWord := ""
scan := time.Now()
for scanner.Scan() {
tkLen := len(scanner.Text())
if tkLen >= max {
max = tkLen
longestWord = scanner.Text()
}
}
end := time.Now()
fmt.Printf("Longest word is %d characters\n", max)
fmt.Printf("Longest word is %s\n", longestWord)
fmt.Println("Total time:\t\t", end.Sub(start).Nanoseconds()/1000000)
fmt.Println("Scan time:\t\t", end.Sub(scan).Nanoseconds()/1000000)
}