I implemented a ListView operation with custom images and had the following code:
@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); .... canvas.drawText(String.format("%02d: %dx%d", position, w, h), 10, 15, cached_paint); }
There was almost nothing in the onDraw method, so it drove me crazy about why scrolling was so poor. I accidentally changed the drawText parameter to not use String.format, and suddenly the scroll was oily silk again. In fact, almost everything is the same, but it works well:
canvas.drawText("" + position + ": " + w + "x" + h, 10, 15, cached_paint);
I am stunned. Why is the latter faster than calling String.format? I would expect that concatenating an object would create more intermediate objects and generally garbage performance, but I found the exact opposite. In fact, when working with String.format, I received a lot of messages about distribution / release from vm.
So why is String.format so slow when it can apparently be faster (at least when switching from other programming languages ββwhere creating an object is expensive)?
source share