Category Archives: Uncategorized

ArrayList vs LinkedList [java]

Today I was reviewing the Collection’s chapter for the SCJP (Sun Certified Java Programmer) and for the sake of understanding, I started playing around with ArrayList and LinkedList implementation of the java.lang.List interface. I was curious about the differences (here an interesting post) and I wrote a small piece of code to measure the time needed to perform some basic operations: add(), get(), contains(), remove().

package collection;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: lfoppiano
 * Date: 14/10/12
 * Time: 16:37
 * To change this template use File | Settings | File Templates.
 */
public class ListTest {

    private static final int NUMBER_OF_ELEMENTS = 1000000;

    public static void main(String... args) {

        List arrayList = new ArrayList();
        System.out.println("Array list benchmark");

        BenchmarkList benchmarkList = new BenchmarkList(arrayList, NUMBER_OF_ELEMENTS);
        benchmarkList.runAllTests();

        List linkedList = new LinkedList();
        System.out.println("Linkedlist benchmark");

        benchmarkList = new BenchmarkList(linkedList, NUMBER_OF_ELEMENTS);
        benchmarkList.runAllTests();
    }
}

class BenchmarkList {

    private List target;
    private Integer numberOfElements;

    BenchmarkList(List target) {
        this.target = target;
    }

    BenchmarkList(List target, Integer numberOfElements) {
        this(target);
        this.numberOfElements = numberOfElements;
    }

    public void runAddTest() {
        long start = System.currentTimeMillis();
        for (int x = 0; x < numberOfElements; x++)
            target.add("a");

        long end = System.currentTimeMillis();
        System.out.println("Insert of " + numberOfElements + " elements: " + (end - start) + " ms");

    }

    public void runRemoveTest() {
        long start = System.currentTimeMillis();

        target.removeAll(target);

        long end = System.currentTimeMillis();
        System.out.println("Remove of " + numberOfElements + " elements: " + (end - start) + " ms");
    }

    public void runAccessTest() {
        long start = System.currentTimeMillis();
        for (int x = 0; x < numberOfElements; x++)
            target.get(x);

        long end = System.currentTimeMillis();
        System.out.println("Access of " + numberOfElements + " elements: " + (end - start) + " ms");
    }

    public void runContainsTest() {
        long start = System.currentTimeMillis();
        target.contains("342332432");

        long end = System.currentTimeMillis();
        System.out.println("Contains of " + numberOfElements + " elements: " + (end - start) + " ms");
    }

    public void runAllTests() {
        runAddTest();
        runContainsTest();
        runAccessTest();
        runRemoveTest();
    }
}

And this is the result with a case of 100, 1000, 10000 and 100000 elements:

Some comments:

  • ArrayList are fast to iterate to random access the data (get() an element). adding or removing element are more expensive, because, for the nature of an Array, the elements needs to be reorganized.
  • LinkedList are faster to add() and remove() because every element is linked to the following and the previous. The access is double penalized because there is no index as in the ArrayList, but to get to the element it is required to iterate the entire list.
  • ArrayList are 90% of the cases good enough, however for heavy duty operations of insertion and removal, LinkedList is a more appropriate choice. I must say that the gain on the insert/remove is counterbalanced by a huge penalty on random access, therefore the usage of LinkedList should be carefully verified, to avoid bad surprises.
  • Accessing elements with an Iterator bring benefit only on LinkedList, on the other hand, we are not talking anymore about random access.
About these ads

iPhone 5? also not …

iPhone 4S

The bigger thing happens to 

iPhone since iPhone 5

Something to read:

Update 14/10/2012@23:25: Using apple maps make me feel stuck. I cannot plan an itinerary from Milan to Cervinia (famous town in North-Italy famous for the skiing tourism), because… it doesn’t find it! WTF!

Fast test, slow test

I found this video quite interesting, here some observations:

  • Why you test? Tests are preventing regression, fear (enable refactoring), bad design.
  • In the video the guy is talking about System tests. The meaning is quite generic for integration and/or functional tests. Normally they are not as fast as Unit tests.
  • If every time you change the code, you have to change the tests, you have clearly problems with tests
  • A way to measure how our test is unit is to measure how many dependencies we have in every of our tests. Every dependency increase the probability to fail the tests if external code is changed, and when it fails cannot tell you exactly where.

Reasons to fail the test strategy:

  • Use selenium as primary test suite – slow – no possibility to use TDD
  • Unit tests are too big. Studies have demonstrate that testing time is growing exponentially from the start of a process development.
  • Write fine grained tests around legacy code is a bad way to design tests.

Suggestions:

  • 90-95% of Unit tests – 5-10% Integration/Functional tests
  • Quick unit Tests allow you to do TDD. If it has to fail, it has to fail fast.
  • When unit tests are failing, they are pointing the fine grain problem, if this is not true, they are not unit tests

How I would organize a meetup event within my company

What I’m writing,  should not considered nothing more than a recommendation.

A couple of months ago I was talking about the last meetup organized within our company and I get the opportunity to share some ideas and feedback about it. Every year the organizers are trying to get something new out of the hat; perhaps risks and surprises are never missing. Anyway no matter how the general feeling is, because behind is always required a lot of work to put all the pieces together.

Now, I asked myself a question: how would I organize this event in my company?

It’s easy to complain with just bla bla… but getting yourself in the middle of the action is the best way to consider and see all the variables that are part of the ‘game’. A citation just pop up in my mind, during my activities within Fedora, from Jeroen Van Meeuwen: “Stand up or shut up”. Let’s try to stand up :)

First of all, what is the goal of the meetup? I see three important components in it:

  • social
  • visionary
  • technical

The social component is fundamental to keep in touch people working together but not on the same working place. To do that in a peaceful way is common to have ‘team building events’, group activities aimed to keep people together on something interesting and non work related activity.

The visionary component is a communication from the company to the employees. A keynote given by the president or CEO of the company with a recap of last year’s achievements, and a projection, presentation of  new year ideas, vision, directions is going to be followed. This could be followed by a SWAT/Brainstorming session, with all the members with the objective of continuous improvement of the processes and operations within the company.

The technical component should help people to exchange information about what they are doing, what they have done, to have some expertise and skills transfer, to motivate and to speed up the growing process of the youngest. This can be done with talks proposed by members of the company on specific and technical subjects.

In the past I have seen a lot of changes from the organisers, trying to balance the amount of time spent on each activity, however I think that all three components should be present, although the technical component might be out of scope in case nobody would propose anything or the arguments would not be enough interesting.

Sardegna!

U cannonau! #italy #food #landscape #sardinia (Taken with Instagram)