stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.54k stars 2.56k forks source link

improve string parsing performance #900

Closed Simulant87 closed 4 months ago

Simulant87 commented 4 months ago

implementation of #899

performance test implementation below.

Measurement on master branch:

39151ms for 100 iterations of parsing JSONObject. 391ms per parsing on average.

Measurement on improved branch:

32337ms for 100 iterations of parsing JSONObject. 323ms per parsing on average.

--> 18% performance improvement.

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {

    public static void main(String[] args) throws IOException {
        // TODO: update path to 30mb input file train-v1.1.json from https://www.kaggle.com/datasets/stanfordu/stanford-question-answering-dataset
        InputStream inputStream = new FileInputStream("C:/JSON-java/train-v1.1.json");
        String content = inputStreamToString(inputStream);
        JSONObject object = new JSONObject(content);
        // JVM warm up: 10 iterations
        for (int i = 0; i < 10; i++) {
            object = new JSONObject(content);
            System.out.println("warmup: " + i);
        }

        // measured runs
        long start = System.currentTimeMillis();
        int iterations = 100;
        for (int i = 0; i < iterations; i++) {
            object = new JSONObject(content);
            System.out.println("test: " + i);
        }
        long end = System.currentTimeMillis();

        long totalTime = end - start;
        System.out.println(totalTime + "ms for " + iterations + " iterations of parsing JSONObject. " + totalTime / iterations + "ms per parsing on average.");
    }

    private static String inputStreamToString(InputStream inputStream) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString(StandardCharsets.UTF_8.name());
    }

}
stleary commented 4 months ago

Closed due to using a modified version of a GPLv2-licensed file (or possibly a modified Oracle-proprietary file, I could not tell from the comments). In general, this project does not allow use of modified licensed files.