Slow Converting from JsonArray to ArrayList

Posted on

Problem

I have to convert a JsonArray to an ArrayList but unfortunately the code below is used very often during the execution of the program! How can I speed it up?

Java:

public static ArrayList CastJsonObject(JSONArray object) {
        ArrayList<Cantiere> tm = new ArrayList<Cantiere>();
        Cantiere cant;
        Cliente c;
        try {
            for (int i = 0; i < object.length(); i++) {
                JSONObject value = object.getJSONObject(i);
                int IdCantiere = Integer.parseInt(value.getString("IdCantiere").toString());
                int IdCliente = Integer.parseInt(value.getString("IdCliente").toString());
                String Filiale = value.getString("Filiale").toString();
                String RagioneSociale = value.getString("RagioneSociale").toString();
                String NomeCantiere = value.getString("NomeCantiere").toString();
                String DataCreazioneCantiere = value.getString("DataCreazioneCantiere").toString();
                String Tipologia = value.getString("Tipologia").toString();
                String StatoCantiere = value.getString("StatoCantiere").toString();
                int StatoFatturazione = Integer.parseInt(value.getString("StatoFatturazione").toString());
                c = new Cliente(IdCliente, RagioneSociale, Filiale);
                cant = new Cantiere(c, IdCantiere, NomeCantiere, DataCreazioneCantiere, Tipologia, StatoCantiere, StatoFatturazione);
                tm.add(cant);
            }
        } catch (Exception ex) {
            System.out.println("n Error: " + ex);
        }

        return tm;
    }

Solution

If there really is a performance problem with that code, please back this up with some measurements. Otherwise, read some articles concerning premature optimization and don’t bother.

Nevertheless, on micro-level there are a few calls that you don’t need:

Get rid of superfluous .toString() calls, e.g. replace

String Filiale = value.getString("Filiale").toString();

with

String Filiale = value.getString("Filiale"); // this already is a string

Similarly, don’t parse the integers yourself after converting them to string, but instead call getInt() on the Json object:

int IdCantiere = value.getInt("IdCantiere");

Avoid the repeated call to object.length() by holding the length in a variable:

for(int i = 0, n = object.length(); i < n; i++)

Still, the code should be relatively fast as-is. Naturally, there’s no way to be really sure without knowing what the constructors for Cliente and Cantiere actually do. If you have a real performance problem, introduce measurements aroud the parameter extraction and the object construction.

And: measure BEFORE AND AFTER you change the code as shown above so that you actually know what effect it had. See https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java for some advice on how to do this.

Leave a Reply

Your email address will not be published. Required fields are marked *