免责声明:我不想讨论JSON和XML。真的。
我做了以下基准,我想知道我是不是完全错了:
我制作了一个简单的Java POJO模型(一个包含学生列表以及主题列表和一些属性的课程)。它看起来像这样:
public class Course {
private String name;
private String description;
private int rating;
private List< Student > students = new ArrayList<>();
private List< Topic > topics = new ArrayList<>();
// getter and setter...
}
public class Student {
...
}
public class Topic {
...
}
我创建了一个课程实例,让我们说10.000个学生和10.000个主题并用随机值初始化它:
Course course = new Course();
for ( int i = 0 + t; i < 10000 + t; i++ ) {
Student student = new Student();
student.setAge( ( int ) ( 100 * Math.random() ) + t );
student.setName( UUID.randomUUID().toString() );
course.getStudents().add( student );
}
for ( int i = 0 + t; i < 10000 + t; i++ ) {
Topic topic = new Topic();
topic.setDescription( UUID.randomUUID().toString() );
topic.setDifficulty( ( int ) ( 10 * Math.random() ) + t );
topic.setName( UUID.randomUUID().toString() );
topic.setRating( ( int ) ( 10 * Math.random() ) + t );
course.getTopics().add( topic );
}
我将这个Course
对象写入文件(循环中500次)并使用XML和JSON测量时间。我从文件中读取对象(循环中500次),也使用XML和JSON。例如,下面是我用来将对象编写为JSON的代码和我用来将其编写为XML的代码:
Writer writer = new FileWriter( this.jsonFile );
Gson gson = new GsonBuilder().create();
Course course = createCourse( i );
gson.toJson( course, writer );
writer.close();
JAXBContext jaxbContext = JAXBContext.newInstance( Course.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, false );
Course course = createCourse( i );
jaxbMarshaller.marshal( course, this.xmlFile );
我在课程对象中使用了200,2000,2.000,100.000和200.000名学生和主题。结果如下:
包含更多详细信息的Google文档位于https://docs.google.com/spreadsheets/d/1t6tRfja6cKEnl7oYxSa69bwDvIcDniy9lK5rzOZqb30/edit?usp=sharing
我用过:
我的结果有合理的解释吗?我的测量中是否犯了明显的错误?或者只是巧合?