Monday, December 14, 2009

Java For-Loop Effeciency: .size()

Many majors in most colleges require Comp Sci to some degree, generally teaching java. I have always found the underlying pinnings of languages very interesting, so this may bore some of my readers, and some, it may catch your interest.

This makes perfect sense, but I do not think many people realize it. It is better to allocate a variable outside a for loop as apposed to using a .size method inside it. Of course, being ever curious, I decided to test this. Bellow is the code I made (yes, its bad, but it was written quickly.)


import java.util.ArrayList;


public class Tester {


public static void main(String []argv) {

ArrayList test = new ArrayList();

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

test.add("a");

test.add("b");

System.out.println("The array has " + test.size() + " objects");

long startTime = System.nanoTime();

withSize((ArrayList)test.clone());

long timeDiff = System.nanoTime() - startTime;

System.out.println("With bad coding style :" + timeDiff + "");

long startTime2 = System.nanoTime();

withoutSize((ArrayList)test.clone());

long timeDiff2 = System.nanoTime() - startTime2;

System.out.println("With good coding style :" + timeDiff2 + "");

}

public static void withSize( ArrayList ar) {

for(int a = 0; a<ar.size()-1; a++) {

ar.set(a,ar.get(a)+ar.get(a+1));

}

}

public static void withoutSize( ArrayList ar) {

//Notice that ar.size() is not in the loop

int b = ar.size();

for(int a = 0;a<ar.size()-1;a++) {

ar.set(a,ar.get(a)+ar.get(a+1));

}

}

}

You will find pretty consistently that the second loop executes quicker. As for memory allocation assigning int b outside a loop, an int is a 32 bit object, which is really not much considering how big we make our memory and that it will be dealloc'd after the method completes. Of course, the time difference is in nanoseconds, but over time I am sure the for loops in code do build up, especially as code gets longer and longer.

An important, almost ironic note, this will not work on Windows, the timer does not have a good enough resolution.

Sunday, December 6, 2009

Midnight Maddness

So, its midnight again. Half awake, half asleep, more homework to do, but don't want to do it. I am sure everyone here has had that feeling of oncoming dread that is work, or even worse, last minute assignments before exams.

So what is my solution? Google!

Yep, thats right, google to the rescue! Google is the mastermind of anything. Is there anything google cannot do?

Give it another year, and we wont even need to click dial to order pizza, Google will know our preferences via our search habits, we will just search it and google will call and order pizza for us! Google pioneers everything. Try to go a full week without hearing something about google. Lets just face it, Google is awesome, and the ultimate distraction!

Wednesday, December 2, 2009

Weave: The future of cross-computer browsing

Every browser implements bookmarks in some way, and almost every user type, despite their technical skill levels, use them. Browsers are made initially an audience, and to shape into a perfect fit for a person.

What makes firefox so great? Why do people love it so much?

Personalization

Thats right, the ability to add their own plugins, and set up features just the way they like it. Every time I use internet explorer or safari on a friends computer, it almost always feels the same, with firefox, it feels different. Every time you open it up, the user has different plugins, different awesome-bar results, themes, etc. Google knows that the browser is a personal experience, hence their implementation of the uni-bar idea in the Google Chrome browser.

But I digress, my point is, that the browser becomes a personal experience, it knows what you commonly open, it can save passwords, it can hold bookmarks, and when you go to a different computer, it just does not feel the same. So how do you make your computers feel the same, when you go from your desktop to your laptop, how do you keep everything you have on one with the other. The answer is simple: Weave.

Weave is a plugin for Mozilla Firefox being developed in Mozilla Labs. It allows you to sync *everything* in Firefox, from search results, to bookmarks, to your open tabs, to your passwords, and whats more, it does it securely. Thats right, a completely secure way to transfer all of your data.

For the more technical readers, the security system goes something like this. You create a name and a password on the server so you can retrieve your data. Then your data is encrypted by a locked key. When you try to retrieve your data, it first sends you the key that you must unlock with a super secret passphrase. Everything you send is encrypted before it hits the server and is decrypted only at the end computer. The server has no way to read the data since it never sees your passphrase.

Right now, this program is still in heavy development, eventually it will let you sync plugins and eventually even sync your bookmarks with mobile devices. Everything is open and new ideas are as always welcome to this project. I highly recommend everyone takes a look at this. Worst comes to worse, if your computer crashes, your bookmarks will be safe. :P