Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, June 18, 2016

Java 8. What is cost of lambdas ?

I like the lambdas. From c# of course, but now is possible to use lambdas also in Java 8.
But what is performance ?
  1. Compare short list of strings with comparator defined as class
  2. Compare short list of strings with comparator defined as anonymous class
  3. Compare short list of strings with comparator defined as lambda
Results:
  1. 47563
  2. 78857
  3. 111320
So lambda comparator is  2.34 time slower than comparator defined by class, and 1.65 times slower than comparator defined by anonymous class.

And here is code:


package com.company;

import java.util.*;

public class Main {

    public static void main(String[] args) {

        CompareByClass();

        CompareByLambda();

        CompareByAnonymous();
    }
    private static void CompareByClass() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, new myComparator());
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }

    private static void CompareByAnonymous() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, new Comparator<String>() {
                @Override
                public int compare(String a, String b) {
                    return b.compareTo(a);
                }
            });
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }

    private static void CompareByLambda() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Date timeStart = new Date();
        for(int i=0; i<Integer.MAX_VALUE;i++) {
            Collections.sort(names, (String a, String b) -> b.compareTo(a));
        }
        Date timeStop = new Date();
        System.out.println(timeStop.getTime() - timeStart.getTime());
    }
}

package com.company;

import java.util.Comparator;

public class myComparator implements Comparator<String>
{
    public int compare(String a, String b) {
        return b.compareTo(a);
    }
}

Monday, June 13, 2016

Java: What is cost of casting and reflection ?

Simple question: What is cost of casting or using dynamic ?

.. but that was c# . What about Java ? No dynamics so we should use reflection.

Answers:
  1. Simple Counter
    • Counter as long (miliseconds):
      • 264
    • Counter as object, and cast to long: 
      • 13484
    •  Difference:
      •  casting from object to long is 51 times slower .
  2. Simple class with int property
    • No casting
      • 256
    • casting from object
      • 252
    • used as reflection
      • 29360114
    • Difference
      • casting from object to class is just .. 1.0158 times ... faster ?
      • but reflection ... is 114687.94 times slower !
Code:


package castingandgenerics;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class program {
 
 public static void main(String [ ] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
 {
  objectAndLongComparison();
  objectAndClassComparison();
  
 }
 
    private static void objectAndLongComparison()
    {
        long longCounter = 0;
        Object objectCounter = longCounter;
        Date timeLongStart, timeLongStop, timeObjectStart, timeObjectStop;
        System.out.println("Object and Long comparison:");
        timeLongStart = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            longCounter = longCounter + 1;
        }
        timeLongStop = new Date();
        System.out.println(timeLongStop.getTime() - timeLongStart.getTime());

        timeObjectStart = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            objectCounter = ((long)objectCounter) + 1;
        }
        timeObjectStop = new Date();
        
        System.out.println(timeObjectStop.getTime() - timeObjectStart.getTime());
    }
    
    private static void objectAndClassComparison() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
    {

        Date[] timeStart = new Date[3];
        Date[] timeStop = new Date[3];
        Class1 myClass = new Class1();
        Object myObject = myClass;
      //  dynamic myDynamic = myClass;

        System.out.println("Object and Class comparison:");
        timeStart[0] = new Date();

        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            myClass.setMyProperty1(i);
            //myClass.MyProperty2 = string.Empty;
        }
        timeStop[0] = new Date();
        System.out.println(timeStop[0].getTime() - timeStart[0].getTime());

        timeStart[1] = new Date();
        for (int i = 0; i < Integer.MAX_VALUE; i++)
        {
            ((Class1)myObject).setMyProperty1(i);

        }
        timeStop[1] = new Date();
        System.out.println(timeStop[1].getTime() - timeStart[1].getTime());

        timeStart[2] = new Date();
        Class[] cArg = new Class[1];
        cArg[0] = int.class;
        for (int i = 0; i < 1024 ; i++)
        {
         Method method = myObject.getClass().getMethod("setMyProperty1",cArg[0]);
            method.invoke(myObject, i);

        }
        timeStop[2] = new Date();
        System.out.println((timeStop[2].getTime() - timeStart[2].getTime())*(Integer.MAX_VALUE/1024));

    }
}

Code is also here (GitHub).