-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
131 lines (113 loc) · 2.73 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"math"
"os"
"runtime"
"runtime/pprof"
"time"
)
type revisionFunc func(string, io.Writer) error
var revisionFuncs = []revisionFunc{r1, r2, r3, r4, r5, r6, r7, r8, r9, r10}
var maxGoroutines int
func main() {
var (
cpuProfile = flag.String("cpuprofile", "", "write CPU profile to file")
revision = flag.Int("revision", len(revisionFuncs), "revision of solution to run")
goroutines = flag.Int("goroutines", 0, "num goroutines for parallel solutions (default NumCPU)")
benchAll = flag.Bool("benchall", false, "benchmark all solutions")
)
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: go-r1bc [-cpuprofile=PROFILE] [-revision=N] INPUTFILE\n")
flag.PrintDefaults()
}
flag.Parse()
if *revision < 1 || *revision > len(revisionFuncs) {
fmt.Fprintf(os.Stderr, "invalid revision %d\n", *revision)
os.Exit(1)
}
maxGoroutines = *goroutines
if maxGoroutines == 0 {
maxGoroutines = runtime.NumCPU()
}
args := flag.Args()
if len(args) < 1 {
flag.Usage()
os.Exit(2)
}
inputPath := args[0]
st, err := os.Stat(inputPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
size := st.Size()
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *benchAll {
err := benchmarkAll(inputPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
return
}
start := time.Now()
output := bufio.NewWriter(os.Stdout)
rf := revisionFuncs[*revision-1]
err = rf(inputPath, output)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
output.Flush()
elapsed := time.Since(start)
fmt.Fprintf(os.Stderr, "Processed %.1fMB in %s\n",
float64(size)/(1024*1024), elapsed)
}
func benchmarkAll(inputPath string) error {
const tries = 5
var buf bytes.Buffer
err := r1(inputPath, &buf)
if err != nil {
return err
}
expected := buf.String()
var r1Best time.Duration
for i, rf := range revisionFuncs {
fmt.Fprintf(os.Stderr, "r%d: ", i+1)
bestTime := time.Duration(math.MaxInt64)
for try := 0; try < tries; try++ {
var output bytes.Buffer
start := time.Now()
err := rf(inputPath, &output)
if err != nil {
return err
}
elapsed := time.Since(start)
fmt.Fprintf(os.Stderr, "%v ", elapsed)
bestTime = min(bestTime, elapsed)
if i == 0 {
r1Best = bestTime
}
if output.String() != expected {
return fmt.Errorf("r%d didn't give correct result", i+1)
}
}
fmt.Fprintf(os.Stderr, "- best: %v (%.2fx as fast as r1)\n",
bestTime, float64(r1Best)/float64(bestTime))
}
return nil
}