Created
December 14, 2010 10:55
-
-
Save ussy/740271 to your computer and use it in GitHub Desktop.
JSON Benchmark
This file contains hidden or 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.text.SimpleDateFormat; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.List; | |
| import java.util.Locale; | |
| import net.arnx.jsonic.JSON; | |
| import net.arnx.jsonic.JSONHint; | |
| import org.codehaus.jackson.map.ObjectMapper; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| public class JsonMain { | |
| public static void main(String[] args) throws Exception { | |
| Result result = createResult(1000); | |
| // すぶり | |
| long start = System.currentTimeMillis(); | |
| ObjectMapper mapper = new ObjectMapper (); | |
| mapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)); | |
| mapper.writeValueAsString(result); | |
| long end = System.currentTimeMillis(); | |
| System.out.println("Jackson:" + (end - start)); | |
| start = System.currentTimeMillis(); | |
| Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create(); | |
| gson.toJson(result); | |
| end = System.currentTimeMillis(); | |
| System.out.println("Gson:" + (end - start)); | |
| start = System.currentTimeMillis(); | |
| JSON.encode(result); | |
| end = System.currentTimeMillis(); | |
| System.out.println("JSONIC:" + (end - start)); | |
| // | |
| System.out.println("----"); | |
| final int count = 100; | |
| long jacksonTime = 0; | |
| long gsonTime = 0; | |
| long jsonicTime = 0; | |
| for (int i = 0; i < count; i++) { | |
| start = System.currentTimeMillis(); | |
| mapper = new ObjectMapper(); | |
| mapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)); | |
| mapper.writeValueAsString(result); | |
| end = System.currentTimeMillis(); | |
| jacksonTime += (end - start); | |
| start = System.currentTimeMillis(); | |
| gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create(); | |
| gson.toJson(result); | |
| end = System.currentTimeMillis(); | |
| gsonTime += (end - start); | |
| start = System.currentTimeMillis(); | |
| JSON.encode(result); | |
| end = System.currentTimeMillis(); | |
| jsonicTime += (end - start); | |
| } | |
| System.out.println("Jackson:" + (jacksonTime / count)); | |
| System.out.println("Gson:" + (gsonTime / count)); | |
| System.out.println("JSONIC:" + (jsonicTime / count)); | |
| } | |
| static Result createResult(long num) { | |
| Result result = new Result(); | |
| for (long i = 0; i < num; i++) { | |
| Person person = new Person(); | |
| person.id = i; | |
| person.age = (int) (i % 100); | |
| person.name = "たろう" + i; | |
| person.birthday = new Date(System.currentTimeMillis() + i); | |
| List<Person> children = new ArrayList<Person>(); | |
| for (int j = 0; j < 5; j++) { | |
| Person child = new Person(); | |
| child.id = j; | |
| child.age = (int) (i % 100); | |
| child.name = "たろう" + i; | |
| child.birthday = new Date(System.currentTimeMillis() + i); | |
| } | |
| person.children = children; | |
| result.persons.add(person); | |
| } | |
| return result; | |
| } | |
| static class Result { | |
| public List<Person> persons = new ArrayList<Person>(); | |
| } | |
| static class Person { | |
| public long id; | |
| public int age; | |
| public String name; | |
| @JSONHint(format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") | |
| public Date birthday; | |
| public List<Person> children = new ArrayList<Person>(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment