-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (47 loc) · 896 Bytes
/
main.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
47
48
49
50
51
52
53
54
55
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/cheggaaa/pb/v3"
mp "github.com/edsrzf/mmap-go"
"log"
"os"
"time"
)
func main() {
filePath := flag.String("f", "text.txt", "file path to read from")
flag.Parse()
//Open file
f, err := os.OpenFile(*filePath, os.O_RDONLY, 0644)
if err != nil {
log.Fatal(err)
return
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
fi, _ := f.Stat()
bar := pb.Full.Start64(fi.Size())
//mmap
np, err := mp.Map(f, mp.RDONLY, 0)
defer np.Unmap()
if err != nil {
panic(err)
}
reader := bar.NewProxyReader(bytes.NewReader(np))
s := bufio.NewScanner(reader)
startTime := time.Now()
count := 0
for s.Scan() {
count++
}
endTime := time.Now()
bar.Finish()
spent := endTime.Sub(startTime)
fmt.Printf("Line count:%d \n", count)
fmt.Printf("Time:%d sec\n", int(spent.Seconds()))
}