Created
December 16, 2010 14:36
-
-
Save ussy/743456 to your computer and use it in GitHub Desktop.
Gson
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.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
Result result = createResult(1000); | |
// すぶり | |
long start = System.currentTimeMillis(); | |
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create(); | |
gson.toJson(result); | |
long end = System.currentTimeMillis(); | |
System.out.println("Gson:" + (end - start)); | |
// | |
final int count = 100; | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < 100; i++) { | |
gson.toJson(result); | |
} | |
end = System.currentTimeMillis(); | |
System.out.println("Gson:" + ((end - start) / 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); | |
children.add(child); | |
} | |
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; | |
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