java - Why Jackson is so slow? -
i use simple jackson code convert object json string, slow. takes 70 milisecond convert 1 object below in machine. did wong?
objectmapper myobjectmapper = new objectmapper(); void testjson() { myclass state = new myclass(); try { string result = myobjectmapper.writevalueasstring(state); } catch (exception ex) { } }
myclass 4 members
myclass { public int a; public int b; public int c; public string d; }
i use simple jackson code convert object json string, slow. takes 70 milisecond convert 1 object below in machine. did wong?
70 milliseconds encode , send json message implausible. there little doubt in mind real explanation 70 millisecond measurement artefact of way benchmarked code. probably, didn't allow jvm warm-up effects.
so, yes, did benchmarking incorrectly. probably.
now found out 3 solutions sending myclass's instance on network: 1: use jackson convert byte[] , send. 2: use built-in serialization convert byte[] , send. 3: convert myclass's members string, byte[] , send. there other better solution ?
in theory (i.e. if have enough skill, time , patience) best performance can achieved encoding data hand byte buffer , sending that. theoretical.
if looking practical solutions potentially faster alternatives have tried, take @ google protocol buffers. reputed fast ...
70 milliseconds 1 object, when try 1000 objects, takes 114 milliseconds, isn't strange?
actually, not @ strange when take account of way jvm works.
when jvm starts, loads code , starts running using bytecode interpreter. after code has been interpreted bit, jvm runs jit compiler produce optimized native code methods being called frequently. compilation process takes significant amount of time.
so when measuring time taken send 1 object, really measuring time send 1 object , bunch of jit compilation. jit compilation work won't need repeated. net result - processing 1 object appears takes surprising long time, compared 1000.
jit compilation 1 of common jvm warmup effects can distort poorly written java benchmark.
Comments
Post a Comment