From 93e199bd3382a684946c5666baf740417700bf28 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 10 Oct 2019 10:37:50 -0400 Subject: [PATCH] Report average frame time. --- fast/main.go | 16 +++++++++++++++- slow/main.go | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/fast/main.go b/fast/main.go index 6ced490..68ec320 100644 --- a/fast/main.go +++ b/fast/main.go @@ -39,6 +39,8 @@ func loop(w *app.Window) error { q := w.Queue() face := faces.For(regular, ui.Sp(16)) lst := layout.List{Axis: layout.Vertical} + times := make([]int64, 100) + timesIndex := 0 for { e := <-w.Events() switch e := e.(type) { @@ -54,7 +56,19 @@ func loop(w *app.Window) error { } lst.Layout() w.Update(ops) - log.Printf("Frame time: %s\n", time.Since(stime)) + dur := time.Since(stime) + //fmt.Printf("Frame time: %s\n", dur) + times[timesIndex] = dur.Nanoseconds() + timesIndex++ + if timesIndex == 100 { + timesIndex = 0 + var avg int64 + for i := 0; i < 100; i++ { + avg += times[i] + } + avg = avg / 100000 + fmt.Printf("Average frame time: %d microseconds\n", avg) + } } } } diff --git a/slow/main.go b/slow/main.go index 22c1f4b..494554e 100644 --- a/slow/main.go +++ b/slow/main.go @@ -37,6 +37,8 @@ func loop(w *app.Window) error { family := &shape.Family{Regular: regular} gtx := &layout.Context{Queue: w.Queue()} lst := &layout.List{Axis: layout.Vertical} + times := make([]int64, 100) + timesIndex := 0 for { e := <-w.Events() switch e := e.(type) { @@ -51,7 +53,19 @@ func loop(w *app.Window) error { }) } w.Update(gtx.Ops) - log.Printf("Frame time: %s\n", time.Since(stime)) + dur := time.Since(stime) + //fmt.Printf("Frame time: %s\n", dur) + times[timesIndex] = dur.Nanoseconds() + timesIndex++ + if timesIndex == 100 { + timesIndex = 0 + var avg int64 + for i := 0; i < 100; i++ { + avg += times[i] + } + avg = avg / 100000 + fmt.Printf("Average frame time: %d microseconds\n", avg) + } } } }