29 May 2009

Python is faster than Java!

I wanted to pitch Java and Python against each other and see how they perform. So I put in a simple program that puts some stress on the system. Data Structures would also be used. Both the programs should be run on same machine. There shouldn't be any other Java or Python process running in background. I wrote a simple program in both Java and Python and ran them.

Java Program (JVM version - 1.6.0_11-b03)

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;


public class PerformanceTester {

private final static int SIZE = 500000;

public static void main(String[] args) {

long start_time = System.currentTimeMillis();

Hashtable build_hash = new Hashtable();
for(int i=0; i build_array = new ArrayList();
for(Iterator it = build_hash.keySet().iterator(); it.hasNext();){
Integer value = build_hash.get(it.next());
it.remove();
String strValue = (value.intValue()+8)+"add more";
build_array.add(strValue);
}

long stop_time = System.currentTimeMillis();
System.out.println("Total Time = "+((stop_time - start_time)/1000.0000));

}

}


Multiple Run Result
Total time = 1.391
Total time = 1.375
Total time = 1.344
Total time = 1.422
Total time = 1.484


Python Program (CPython version 2.6.1)

import time

start_time = time.clock()
SIZE = 500000

build_hash = {}

for a in range(1,SIZE):
build_hash[str(a)] = a

build_array = []

for key in build_hash:
build_array.append(str(build_hash[key]+8) + "add more")
build_hash[key] = None

stop_time = time.clock()
print "total time = ", (stop_time - start_time)


Multiple Run Result

Total time = 0.888883333612
Total time = 0.878249380963
Total time = 0.991458537034
Total time = 0.850523498354
Total time = 0.860505161151

I see Python showing a huge advantage when it comes to speed of execution. Java is slow! Thats bad news for Java. I would like you to try same or similar program and let me know how it performs. If you run the same above programs, try increasing the hash size for both the programs. You will be surprised with what you see! I can't believe Java is not only slow, but also uses more memory. Python is not only simpler, but faster, more powerful and uses lesser memory than Java.

28 May 2009

Java is loosing the grip while Python and Ruby gaining ground


We have come accross many instances that Java/J2EE is trying to get rid of its complexities, yet not very successful at it. First the misfortunes EJB 2, and then the obliteration of XML files in a large application. Finally the tedious and daunting task of putting all the types of frameworks and to make them work together for every new application. There has been a huge improvement in EJB 3, but
architects and managers still dread the name EJB.

The management hates it when we tell them that we are throwing away code. They panic, as if we are throwing away used furniture. Why throw it, can't we re-use them somewhere! This statement looks very logical from the management perspective, but when it comes to we super technologists, it sounds ridiculous. For us, old code is like old milk. You refactor it and continually improve it or just throw the freaking thing away. Old code has a shelf life, just like milk. We are involved in developing and maintaining code, we know how bad it stinks. The smell is a good indicator that the application is deteriorating in quality and increasing in quantity.

If you are worried about your job prospects, Java/JEE is still a hot bed as of May 2009. The ecosystem is complex enough that you need people who are bright enough to make sense from it. Many companies still stick to Java as their primary web technology, but things might be changing soon.


Sun is now acquired by Oracle and I do not know what future holds for new Java apps. Oracle is big and they will push the Java technology to upper management in many large corporations. These companies have lot of money to invest and they are always more comfortable when they know that they are in company with large corporation such as Oracle. If you are very enthusiastic about web application development, and are willing to take some risks in the job market, you should try Rails or Django. Job prospects are improving for both, so you might not be in loss. But you will enjoy your work much better because I see either Ruby or Python to be much better programming languages than what Java is or going to become. After being with Java for this long, I see that Java has this inferiority complex with C# and does a catch up. I am in big favor of open source and you might know how I might feel about C#.

Ruby on Rails or Python with Django provide very powerful combination for web development. Job demands will come, but will come as the industry adoption grows. Both Python and Ruby are mature and have good community of users. The new features in the languages come from key persons. Just like Linus Torvalds drives Linux kernel development, Yukihiro Matsumoto will drive Ruby language development and Guido van Rossum will drive Python language development. I feel this is much better than a language being driven through a community similar to JCP.