How to quickly format floating point number in Android game loop?
I'd like to display a millisecond timer value in a game.
String.format
is hellishly slow on Android, so I can't use it. I currently use
this:
long elapsedMillis = ...; //elapsed milliseconds
int whole = (int)(elapsedMillis / 1000);
int fraction = (int)(elapsedMillis % 1000);
StringBuilder sb = new StringBuilder("TIME:");
sb.append(whole);
sb.append('.');
if(fraction < 10)
sb.append("00");
else if(fraction < 100)
sb.append("0");
sb.append(fraction);
g.drawText(sb.toString(), ..开发者_如何学运维.);
but I think it could be done faster. Can you please guide me in the right direction? Thanks
related question
some ideas:
- use a static StringBuilder initialized with a large capacity
- use a native function
- have "TIME:" pre-drawn (I assume it does not move)
- keep the time value in milliseconds
精彩评论