Skip to content

Instantly share code, notes, and snippets.

@benbjohnson
Created April 7, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbjohnson/221a0fcdf76e26eae91a to your computer and use it in GitHub Desktop.
Save benbjohnson/221a0fcdf76e26eae91a to your computer and use it in GitHub Desktop.
Time Generation
$ go test -bench=BenchmarkGenerateTimes
BenchmarkGenerateTimes 5000 261860 ns/op 212992 B/op 1 allocs/op
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