I'm currently parsing JSON and got the following piece of code:

boolean suf = list.getJSONObject(i).getBoolean("sufficient");
String grade = list.getJSONObject(i).getString("grade");
String id= list.getJSONObject(i).getString("id");

I'm wondering if multiple times calling getJSONObject creates overhead resulting in increasing processing time.

Would this be faster and/or better for example?

JSONObject object = list.getJSONObject(i);
boolean suf = object.getBoolean("sufficient");
String grade = object).getString("grade");
String id= object.getString("id");

This does introduce a new object, but will the next 3 calls make the tradeoff worth it?

Since I'm showing a dialog to inform the user something is loading (and thus they can't undertake any action), I'd like to minimize the wait time for the user.

有帮助吗?

解决方案

2nd option is how I usually do. But you will hardly see any notice in performance. list.getJSONObject(i).getBoolean("sufficient"); creates a temporary object and gets the value. Now a days, compilers are smart enough to store that temporary objects just in case them. Even if they don't, unless you are handling some millions of jsonobjs in your "list", I don't see any performance impact here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top