Permalink
Browse files

jackson自定义类与HashMap转字符串性能测试

  • Loading branch information...
1 parent 139bc0a commit 8bb2a25958a3c79dc0a27a493d72996ec5f1363e @zhaodj committed Mar 25, 2013
Showing with 63 additions and 0 deletions.
  1. +3 −0 README
  2. +60 −0 java/demo/src/main/java/org/zhaodj/util/JacksonPerformance.java
View
3 README
@@ -1,2 +1,5 @@
zhaodj's code repository
===============
+
+## TODO
+* gbd调试go程序
@@ -0,0 +1,60 @@
+package org.zhaodj.util;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
+
+/**
+ * 自定义类与HashMap序列化json性能对比
+ * @author zhaodj
+ *
+ */
+public class JacksonPerformance {
+
+ private static ObjectMapper mapper=new ObjectMapper();
+
+ private int code;
+ private String message;
+
+ public int getCode() {
+ return code;
+ }
+
+ public void setCode(int code) {
+ this.code = code;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException{
+ int times=100000;
+ long start=System.nanoTime();
+ for(int i=0;i<times;i++){
+ Map<String,Object> map=new HashMap<String,Object>();
+ map.put("code", 502);
+ map.put("message", "服务器内部错误");
+ mapper.writeValueAsString(map);
+ }
+ System.out.println((System.nanoTime()-start));
+ start=System.nanoTime();
+ for(int i=0;i<times;i++){
+ JacksonPerformance jp=new JacksonPerformance();
+ jp.setCode(502);
+ jp.setMessage("服务器内部错误");
+ mapper.writeValueAsString(jp);
+ }
+ System.out.println((System.nanoTime()-start));
+ }
+
+
+}

0 comments on commit 8bb2a25

Please sign in to comment.