Monday, August 15, 2011

Time cost for creating a HashSet from an ArrayList

I ran this simple test to get a "feel":
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

public class Test {
 public static void main(String... args) {
  List<String> bigList = new ArrayList<String>();
  List<String> smallList = new ArrayList<String>();

  Random random = new Random();

  // builds a 10000 items list made of items
  // with string values generated from random
  // numbers between 1000000 and 1999999
  // this list has a higher chance to have duplicates
  for (int i = 0; i < 10000; i++)
   smallList.add(String.valueOf(random.nextInt(1000000) + 1000000));

  // builds a 1000000 items list made of items
  // with string values generated from random
  // numbers between 1000000 and 1999999
  for (int i = 0; i < 1000000; i++)
   bigList.add(String.valueOf(random.nextInt(1000000) + 1000000));

  int tests = 100;
  
  double smallListTransformationsTotalTime = 0;
  
  for (int i = 0; i < tests; i++) {
   long start = System.currentTimeMillis();
   new HashSet<String>(smallList);
   long end = System.currentTimeMillis() - start;
   smallListTransformationsTotalTime += end;
  }  
  System.out.println("Small list transformation average: " + (smallListTransformationsTotalTime / tests));
  
  double bigListTransformationsTotalTime = 0;
  
  for (int i = 0; i < tests; i++) {
   long start = System.currentTimeMillis();
   new HashSet<String>(bigList);
   long end = System.currentTimeMillis() - start;   
   bigListTransformationsTotalTime += end;
  }
  System.out.println("Big list transformation average: " + (bigListTransformationsTotalTime / tests));  
 }
}
Running this (100 tests) on my computer (i7 quad core using eclipse) yielded the following:

  • Small list transformation averaged 0.81ms 
  • Big list transformation averaged 196.31ms

Wednesday, April 27, 2011

Better late than never

Just a shout out to my dear friend and colleague Shai Raiten for getting his MVP award for the third year in a row.

Shai is an exceptionally gifted professional, engaged in software development, consulting and lecturing on a daily basis. He is also funny as hell and fun to be around... what more can you ask for? :)

So, Mazal tov!!!

P.S See this post for the "official announcement"

Saturday, April 23, 2011

Funky behavior of CSS Universal selector in Google Chrome Developer tools

Although its the middle of the night and I'm in an intense work / implementation session for one of the startups I work with (zbang.it) I had to pause for this.

I was on the brink of loosing my sanity because of this weird problem where styles were merged with each other, when inspecting elements in Google Chrome developer tools (v10.0.648.205).

First, here is the style sheet (or some of it), please note the CSS Universal selector at the beginning:
*
{
    vertical-align: baseline;
    font-weight: inherit;
    font-family: inherit;
    font-style: inherit;
    font-size: 100%;
    border: 0 none;
    outline: 0;
    padding: 0;
    margin: 0;
}

body
{
    font-family: Calibari, Arial;
    font-size: 12px;
}

div.ui-layout-east
{
    width: 250px;
    float: right;
}

div.ui-innerLayout-north
{
    height: 70px;
}

.
.
. 
more css down here
Here is how it looked while inspecting div.ui-layout-east on Google Chrome developer tools:


Take a good look at the highlighted height: 70px, its jumped up from div.ui-innerLayout-north, don't believe me? I didn't believe myself at first, so I added a super noticeable style to div.ui-innerLayout-north, like so:
*
{
    vertical-align: baseline;
    font-weight: inherit;
    font-family: inherit;
    font-style: inherit;
    font-size: 100%;
    border: 0 none;
    outline: 0;
    padding: 0;
    margin: 0;
}

body
{
    font-family: Calibari, Arial;
    font-size: 12px;
}

div.ui-layout-east
{
    width: 250px;
    float: right;
}

div.ui-innerLayout-north
{
    height: 70px;
    border: 3px dashed red;
}

.
.
. 
more css down here
And, lo and behold


It jumped right up again!!!

At first I thought that I had a css syntax error somewhere but by way of elimination and after a long while I found out that this behavior happens only when the universal selector is the first style on the style sheet, and if its location changes:
body
{
    font-family: Calibari, Arial;
    font-size: 12px;
}

*
{
    vertical-align: baseline;
    font-weight: inherit;
    font-family: inherit;
    font-style: inherit;
    font-size: 100%;
    border: 0 none;
    outline: 0;
    padding: 0;
    margin: 0;
}

div.ui-layout-east
{
    width: 250px;
    float: right;
}

div.ui-innerLayout-north
{
    height: 70px;
    border: 3px dashed red;
}

.
.
. 
more css down here
The universe goes to being expected again:


By the way this does not affect the actual HTML in the browser, only the display in developer tools.

Wasted a little over an hour of my life, hope it saves you one.

Thursday, April 21, 2011

Yet Another Facebook Search

Hi,

Check out this website:

http://www.yetanotherfacebooksearch.com

Which helps you search facebook for people, groups and pages. You will also get relevant results about a person and his friends, outside of facebook. This facebook search doesn't requires you to login as well.

Saturday, April 2, 2011

A simple AppEngine Guice Module (Part 1)

This post is about a simple Guice AppEngine module I've written, which I find very useful, hence, I decided to share it.

The code is available here

The module will get your AppEngine project started quickly, here is a typical bootstrap (GuiceServletContextListener) code for a simple app:
package com.codeark.appengine;

import com.codeark.appengine.objectify.ObjectifyModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;

public class Bootstrap extends GuiceServletContextListener {

 @Override
 protected Injector getInjector() {

  Module aeModule = 
   AppEngineModule.build().withDatastoreService()
    .withUrlFetchService().withMemcacheService().withMailService();

  Module myModule = new MyOtherModule();
  
  return Guice.createInjector(aeModule, myModule);
 }
}
The module's Fluent Interface API(which I like so much) exposes a single static build() method which returns an instance of the module. You can then configure which services AppEngine should initialize using the .With*Service() methods.

later you can do:
class Classy {
 @Inject
 public Classy(MemcacheService service, MailService mail) {
 ...
 }
}
There are several other goodies included in the source, which I will discuss in later posts: A simple abstraction over UrlFetchService and an Objectify module.