Created
February 6, 2011 06:44
-
-
Save KengoTODA/813194 to your computer and use it in GitHub Desktop.
Checking about that JSONObject#write(Writer) is more faster than Writer#write(JSONObject#toString()).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NumberOfProperties,toString[ms/1000exec],write[ms/1000exec] | |
100,107,81 | |
200,157,126 | |
300,237,195 | |
400,329,278 | |
500,410,339 | |
600,490,417 | |
700,571,482 | |
800,672,562 | |
900,754,628 | |
1000,838,710 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.FilterOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.util.Arrays; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public class SpeedCheck { | |
private static final int EXEC_TIME = 1000; | |
private static final int TEST_TIME = 20; | |
public static void main(String[] args) { | |
try { | |
new SpeedCheck().test(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void test() throws IOException, JSONException { | |
long[] toStringTime = new long[TEST_TIME]; | |
long[] writeTime = new long[TEST_TIME]; | |
JSONObject object = createJSONObject(1); | |
testToString(object); | |
testWrite(object); | |
for (int j = 100; j <= 1000; j += 100) { | |
object = createJSONObject(j); | |
for (int i = 0; i < TEST_TIME; ++i) { | |
System.gc(); | |
long time = System.currentTimeMillis(); | |
testToString(object); | |
toStringTime[i] = System.currentTimeMillis() - time; | |
System.gc(); | |
time = System.currentTimeMillis(); | |
testWrite(object); | |
writeTime[i] = System.currentTimeMillis() - time; | |
} | |
System.out.printf("%d,%d,%d%n", j, median(toStringTime), median(writeTime)); | |
} | |
} | |
private long median(long[] arr) { | |
Arrays.sort(arr); | |
return (arr[arr.length / 2] + arr[(arr.length - 1) / 2]) / 2; | |
} | |
private void testToString(JSONObject object) throws IOException, | |
JSONException { | |
Writer writer = createWriter(); | |
try { | |
for (int i = 0; i < EXEC_TIME; ++i) { | |
writer.write(object.toString()); | |
} | |
} finally { | |
writer.close(); | |
} | |
} | |
private void testWrite(JSONObject object) throws IOException, JSONException { | |
Writer writer = createWriter(); | |
try { | |
for (int i = 0; i < EXEC_TIME; ++i) { | |
object.write(writer); | |
} | |
} finally { | |
writer.close(); | |
} | |
} | |
private Writer createWriter() throws IOException { | |
return new BufferedWriter(new OutputStreamWriter( | |
new FilterOutputStream(null){ | |
@Override | |
public void write(int i) throws IOException {} | |
@Override | |
public void close() {} | |
})); | |
// return new BufferedWriter(new FileWriter(File.createTempFile("SpeedCheck", ".tmp"))); | |
} | |
private static JSONObject createJSONObject(int num) throws JSONException { | |
JSONObject obj = new JSONObject(); | |
for (int i = 0; i < num; ++i) { | |
obj.append(Integer.toString(i), Integer.toString(i)); | |
} | |
return obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment