Skip to content

Improve performance of toString() methods #54

@vvalchev

Description

@vvalchev

I've played a little bit with the code to improve the performance of toString() methods.

Here is a summary of most important changes:

  • added write(Writer writer, int indentFactor, int indent) to both JSONObject and JSONArray, so now you can use indentation even with the write methods
  • made toString() methods to use the write() method, but writing to a StringWriter. This also reduced the duplicate code!
  • appending to a StringBuffer/StringWriter is synchronized to the StringBuffer. This makes append operations faster.

I've wrote a simple test to measure the performance between changes and here is the results:
D:\develop\json\douglascrockford-JSON-java-d98dc0b>java -cp org.apache.commons.io.jar;. PerfTest
str: 5206
D:\develop\json\douglascrockford-JSON-java-d98dc0b>java -cp org.apache.commons.io.jar;. PerfTest
str: 5241
D:\develop\json\douglascrockford-JSON-java-d98dc0b>java -cp org.apache.commons.io.jar;. PerfTest
str: 5280
D:\develop\json\douglascrockford-JSON-java-d98dc0b>cd ..\new-json
D:\develop\json\new-json>java -cp org.apache.commons.io.jar;. PerfTest
str: 2912
D:\develop\json\new-json>java -cp org.apache.commons.io.jar;. PerfTest
str: 2946
D:\develop\json\new-json>java -cp org.apache.commons.io.jar;. PerfTest
str: 2912

This means that the average milliseconds for the old code is 5242 and 2923 for the new code. This means that the new code is ~1.8 times faster that the original code.

Here is a zip file (http://depositfiles.com/files/vnfv5budt) that includes:

  • updated JSONObject and JSONArray code
  • PerfTest.java
  • screenshot of the output from PerfTest.java

I cannot include the JSON file I've been testing with, but it contains various data types, JSON objects and arrays. It is at about 74K big.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions