Spring Boot, Spring Cloud Lectures
August 30, 2017 Leave a comment
- Spring Boot, Cloud Micro Services Introduction
Software Developer Notes
August 30, 2017 Leave a comment
August 7, 2015 Leave a comment
In this post we will look at simple example how does catching exception impact performance.
private static void exceptionTest(){ int i=0; int j=1; try{ int k=j/i; }catch(ArithmeticException ex){ //not good idea to catch run time exception but catch for demo only } }
private static void withoutExceptionTest(){ int i=0; int j=1; if(i>0){ int k=j/i; } }
private static long exceptionTestLoop(int iterations,boolean isCatchException){ long startTime=System.nanoTime(); for(int i=0;i<iterations;i++){ if(isCatchException){ exceptionTest(); }else{ withoutExceptionTest(); } } long endTime=System.nanoTime(); long time=endTime-startTime; return time; }
public static void main(String[] args) { int itr=1000000; long timeForCatching=exceptionTestLoop(itr,true); long time=exceptionTestLoop(itr,false); System.out.println("Time for catching Exception "+timeForCatching+" without catching "+time); }
Avoid catching exception as it will reduce response time. above example demonstrate results for simple single threaded application but situation become worst in multi threaded environment.
July 31, 2015 Leave a comment
In this post we will compare two famous JSON specific API i.e. GSON and Jackson from performance point of view.
This class is used for conversion to JSON
public class MeasurementRecord {
private String measurementId;
private long duration;
private long time;
private MeasurementType type=MeasurementType.METHOD_CALL;
public MeasurementRecord(String measurementId, long duration, long time,
MeasurementType type) {
super();
this.measurementId = measurementId;
this.duration = duration;
this.time = time;
this.type = type;
}
//getters and setters
}
private static List<String> getList(int iteration){
List l=new ArrayList();
for(int i=0;i<iteration;i++){
l.add(new MeasurementRecord("/test.html", 10, System.currentTimeMillis(), MeasurementType.WEB_REQUEST));
}
return l;
}
private static long jacksonTest(int iteration)throws Exception{
ObjectMapper mapper=new ObjectMapper();
List<String> l=getList(iteration);
long T1=System.nanoTime();
String json=mapper.writeValueAsString(l);
long T2=System.nanoTime();
return (T2-T1);
}
private static long gsonTest(int iteration){
Gson gson = new GsonBuilder().create();
List l=getList(iteration);
long T1=System.nanoTime();
String json=gson.toJson(l);
long T2=System.nanoTime();
return (T2-T1);
}
For converting small or medium size list GSON provide better response as compared to Jackson but for large size list Jackson provide some better response than GSON. Based on this results one can conclude that for converting small or medium size list to JSON one can use GSON for better performance.