From 6b0c318d91068db0cdb6bff54478493154d38244 Mon Sep 17 00:00:00 2001 From: Tim Murray Date: Fri, 1 Nov 2019 15:42:23 -0700 Subject: [PATCH] TouchLatency: don't recreate DecimalFormat on every frame reduces UI thread time by 20% Test: manual Change-Id: If9053d19fc0db1491a0b9402c54243aff8f12432 --- .../prefabulated/touchlatency/TouchLatencyActivity.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java b/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java index ba77a74974d1a..1664746f46368 100644 --- a/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java +++ b/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java @@ -73,6 +73,10 @@ class TouchLatencyView extends View implements View.OnTouchListener { mFps = 0; mLastFpsUpdate = 0; mFrameCount = 0; + + mDf = new DecimalFormat("fps: #.##"); + mDf.setRoundingMode(RoundingMode.HALF_UP); + Trace.endSection(); } @@ -181,9 +185,7 @@ class TouchLatencyView extends View implements View.OnTouchListener { // Draw the ball canvas.drawColor(BACKGROUND_COLOR); canvas.drawOval(left, top, right, bottom, getBallColor()); - DecimalFormat df = new DecimalFormat("fps: #.##"); - df.setRoundingMode(RoundingMode.HALF_UP); - canvas.drawText(df.format(mFps), width, 100, mTextPaint); + canvas.drawText(mDf.format(mFps), width, 100, mTextPaint); invalidate(); Trace.endSection(); @@ -220,6 +222,7 @@ class TouchLatencyView extends View implements View.OnTouchListener { private long mLastDrawNano, mLastFpsUpdate, mFrameCount; private float mFps; + private DecimalFormat mDf; } public class TouchLatencyActivity extends Activity {