Created
April 7, 2015 15:54
-
-
Save benbjohnson/221a0fcdf76e26eae91a to your computer and use it in GitHub Desktop.
Time Generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -bench=BenchmarkGenerateTimes | |
BenchmarkGenerateTimes 5000 261860 ns/op 212992 B/op 1 allocs/op |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tm | |
import ( | |
"testing" | |
"time" | |
) | |
func GenerateTimes(t time.Time, interval, duration time.Duration) []time.Time { | |
n := duration / interval | |
a := make([]time.Time, n) | |
for i := time.Duration(0); i < n; i++ { | |
a[i] = t.Add(i * interval) | |
} | |
return a | |
} | |
func BenchmarkGenerateTimes(b *testing.B) { | |
t := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
GenerateTimes(t, 1*time.Hour, 365*24*time.Hour) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment