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).

Sunday, June 12, 2016

What is cost of casting or dynamic ?

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

Answers:

  1. Simple Counter
    • Counter as long :
      • 00:00:02.6129381
    • Counter as object, and cast to long: 
      • 00:00:30.0840943
    •  Difference:
      •  casting from object to long is 11.51 times slower .
  2. Simple class with int property
    • No casting
      • 00:00:06.3978060
    • casting form object
      • 00:00:07.5841771
    • used as dynamic 
      • 00:01:27.3716838
    • Difference
      • casting from object to class is just 1.185 times slower ..
      • but dynamic.. is 13.65 times slower !
Code:


print 'hello world!'using System;
using System.Diagnostics;

namespace CsharpBoxingUnboxingAndGenericsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            objectAndLongComparison();
            objectAndClassComparison();
        }

        private static void objectAndLongComparison()
        {
            long longCounter = 0;
            object objectCounter = longCounter;
            DateTime timeLongStart, timeLongStop, timeObjectStart, timeObjectStop;
            Trace.WriteLine("Object and Long comparison:");
            timeLongStart = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                longCounter = longCounter + 1;
            }
            timeLongStop = DateTime.Now;
            Trace.WriteLine(timeLongStop - timeLongStart);

            timeObjectStart = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                objectCounter = ((long)objectCounter) + 1;
            }
            timeObjectStop = DateTime.Now;
            
            Trace.WriteLine(timeObjectStop - timeObjectStart);
        }

        private static void objectAndClassComparison()
        {

            var timeStart = new DateTime[3];
            var timeStop = new DateTime[3];
            Class1 myClass = new Class1();
            object myObject = myClass;
            dynamic myDynamic = myClass;

            Trace.WriteLine("Object and Class comparison:");
            timeStart[0] = DateTime.Now;

            for (int i = 0; i < int.MaxValue; i++)
            {
                myClass.MyProperty1 = i;
                //myClass.MyProperty2 = string.Empty;
            }
            timeStop[0] = DateTime.Now;
            Trace.WriteLine(timeStop[0] - timeStart[0]);

            timeStart[1] = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                ((Class1)myObject).MyProperty1 = i;
                //((Class1)myObject).MyProperty2 = string.Empty;
            }
            timeStop[1] = DateTime.Now;
            Trace.WriteLine(timeStop[1] - timeStart[1]);

            timeStart[2] = DateTime.Now;
            for (int i = 0; i < int.MaxValue; i++)
            {
                myDynamic.MyProperty1 = i;
                //myDynamic.MyProperty2 = string.Empty;
            }
            timeStop[2] = DateTime.Now;
            Trace.WriteLine(timeStop[2] - timeStart[2]);

        }
    }
} 

If you would like to play with code here it is on GitHub.

Saturday, June 4, 2016

c#.Virtual, override, new and hidden example.

Are you not sure what virtual , override, new or hidden means ?
No problem , it is easy. Here is example:

Code:

using System.Diagnostics;

namespace VirtualAndNewExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseClass = new BaseClass();
            var derivedVirtual = new DerivedVirtual();
            var derivedNew = new DerivedNew();
            var derivedHidden = new DerivedHidden();

            Trace.WriteLine("Not casted versions");
            baseClass.Name();
            derivedVirtual.Name();
            derivedNew.Name();
            derivedHidden.Name();
            Trace.WriteLine("Casted to base versions");
            baseClass.Name();
            ((BaseClass)derivedVirtual).Name();
            ((BaseClass)derivedNew).Name();
            ((BaseClass)derivedHidden).Name();
        }
    }

    public class BaseClass
    {
        public virtual void Name()
        {
            Trace.WriteLine("Base");
        }
    }

    public class DerivedVirtual : BaseClass
        {
            public override void Name()
            {
                Trace.WriteLine("DerivedVirtual");
            }
        }

    public class DerivedNew : BaseClass
    {
        public new void Name()
        {
            Trace.WriteLine("DerivedNew");
        }
    }

    public class DerivedHidden : BaseClass
    {
        public void Name()
        {
            Trace.WriteLine("DerivedHidden");
        }
    }

}

.. and output (in Output window) :
Not casted versions
Base
DerivedVirtual
DerivedNew
DerivedHidden
Casted to base versions
Base
DerivedVirtual
Base
Base


Now it is clear I hope .
If not you can play with this example also from GitHub.