简体   繁体   中英

Converting Map to Java object using Jackson Object Mapper is very slow

I am currently trying to create 52 Java objects from a Map using the Jackson databinding library and it currently takes a total of 3.518 seconds to convert all 52 objects, which seems to be very slow. I am not sure why this is the case. Is this an anomaly or is there something that can be done to make these conversions faster?

Here is my Java object class:

public class MoodDatapoint extends DocumentModelHelper {

    @JsonProperty(value = "happiness")
    private int happiness;

    @JsonProperty(value = "stress")
    private int stress;

    @JsonProperty(value = "pain")
    private int pain;

    @JsonProperty(value = "timestamp")
    private long timestamp;

    @JsonProperty(value = "lat")
    private double lat;

    @JsonProperty(value = "lng")
    private double lng;

    @JsonProperty(value = "comment")
    private String comment;

    @JsonProperty(value = "event_name")
    private String eventName;

    @JsonProperty(value = "is_before")
    private boolean isBefore;

    @JsonProperty(value = "related_event_id")
    private String relatedEventID;
}

Here is the Map I am trying to convert into the class:

    {
    stress=0, 

    pain=0, 

    happiness=10, 

    timestamp=1488464269384, 

    is_before=false, 

    lng=-79.6208645, 

    event_name=null, 

    comment=, 

    lat=43.6462939,

    related_event_id=null
}

And my code to convert the Map to an object:

ObjectMapper m = new ObjectMapper();

MoodDatapoint datapoint = m.convertValue(map, MoodDatapoint.class);

Using logcat to calculate the duration of each object conversion, it seems like each conversion takes an average of 62 milliseconds:

...
D/M DATAPOINT CONVERSION DURATION: 68
D/M DATAPOINT CONVERSION DURATION: 45
D/TOTAL DURATION (S): 3550
D/AVG DURATION: 68
D/Total objects: 

Do not create ObjectMapper for every conversion, instead create a single instance and use that for all conversions. ObjectMapper creation is a very expensive operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM