/
benchmark.go
96 lines (84 loc) · 2.15 KB
/
benchmark.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
package benchmark
import (
"github.com/disintegration/imaging"
"image"
"time"
)
var filters map[string]imaging.ResampleFilter
func init() {
filters = make(map[string]imaging.ResampleFilter)
filters["NearestNeighbor"] = imaging.NearestNeighbor
filters["Box"] = imaging.Box
filters["Linear"] = imaging.Linear
filters["Hermite"] = imaging.Hermite
filters["MitchellNetravali"] = imaging.MitchellNetravali
filters["CatmullRom"] = imaging.CatmullRom
filters["BSpline"] = imaging.BSpline
filters["Gaussian"] = imaging.Gaussian
filters["Lanczos"] = imaging.Lanczos
filters["Hann"] = imaging.Hann
filters["Hamming"] = imaging.Hamming
filters["Blackman"] = imaging.Blackman
filters["Bartlett"] = imaging.Bartlett
filters["Welch"] = imaging.Welch
filters["Cosine"] = imaging.Cosine
}
type Size [2]int
func (s Size) Eq(c Size) bool {
if s[0] == c[0] && s[1] == c[1] {
return true
}
return false
}
type Results map[string]map[Size]Result
type Result struct {
Min time.Duration
Max time.Duration
Avg time.Duration
}
func (r Result) Compare(e Result) (speed float64) {
return r.Avg.Seconds() / e.Avg.Seconds()
}
func Benchmark(img image.Image, rounds int, targets ...Size) (results Results) {
var (
t time.Time
Δt time.Duration
)
results = make(map[string]map[Size]Result)
for name, filter := range filters {
results[name] = make(map[Size]Result)
for _, target := range targets {
var (
r Result // The result for this filter and size.
total time.Duration
)
for i := 0; i < rounds; i++ {
// Take resizing time
resize:
t = time.Now()
imaging.Resize(img, target[0], target[1], filter)
Δt = time.Since(t)
// FIXME:
// For some reason NearestNeighbor sometimes gets zero duration.
// If this happens, we'll just try again, I guess.
if Δt == 0 {
goto resize
}
// Set min & max if need be.
if r.Min == 0 || Δt < r.Min {
r.Min = Δt
}
if Δt > r.Max {
r.Max = Δt
}
// Accumulate total time.
total += Δt
}
// Calculate average resize time.
r.Avg = total / time.Duration(rounds)
// Append to the results
results[name][target] = r
}
}
return
}