Java, Open Source, Python, Script

Joys of Python and APIs

At my present job , Most of the time, I have to do changes in the legacy Java code to improve performance.

But this time i was asked to tell the extent of performance improvement due to my changes . I tried to use some Java Profiler , I tried Eclipse TPTP and HPROF but due to complacency of the legacy application. I was not able to profile my changes.

So i decided to keep it simple . Log the timestamps and analyze the result. But the log generated by this was unable to show any subsequent improvement in performance as I had to log time in Milliseconds ( System.nanoTime () could not be used for Java 1.4) .

For better time precesion i googled and found this timer library by Vladimir Roubstov.I tweaked it for my code and got the comprehesive log output.

Now to analyze the log, I wrote a python script that reads two log files and generates the time improvements in aggregation and for each run. This was the most exciting part.

for i in imap(lambda x,y:(x, y, x-y), [float(b) for b in new_list],[float(a) for a in old_list]):
              print '%f - %f = %f' % i

I wrote it in no time , Python Rocks . Then I decided to go one step further. What about plotting it on a graph. Straight away Google Chart API came to my mind and there is python wrapper to this.
So I wrote this small function to produce graph for these logs.

def draw_chart():
    from pygooglechart import Axis,SimpleLineChart
    chart = SimpleLineChart(600,200,
                        x_range=(0.000, 0.999), y_range=(0.000, 0.999))
    chart.set_colours(['ff0000', '0000ff'])
    chart.add_data([float(a) for a in old_list])
    chart.add_data([float(b) for b in new_list])
    chart.set_axis_range(Axis.LEFT, 0.000, 0.999)
    chart.set_axis_range(Axis.BOTTOM, 1, 100)'D:\\my_data.png')

This is the graph generated for my data :

The Generated Graph

The graph does not show much improvement though , but the post was about joys of python and APIs :)

P.S. : There are Java backports for using 1.5 > features like System.nanoTime().


Print to console without using semi colon

One of my friend asked : “How can one write to console without using semi colon in Java ?”.
I tried many work-arounds . Even Googling did net help much .

I thought i should raise an exception some how thats it some thing will be written on the console. Naa , its did not work well.

Then a stupid workaround was to enable the verbose argument , thats it every class loaded in the JVM is traces on console.

The some how i tried real tricks to print my name on console ,

This is what i got,

public class Test {

 public static void main(String a[]){

 if ( System.out.append("jasdeep") instanceof Object ){


You might be wondering , why i did not use System.out.println() . Because it does not return anything , So it can not be compared in if block.


When Synchrozation Fails in Java

My blog title goes like “Ramblings about workplace, java, python, linux and web ” . But my recent posts were distracted from this tagline. The frank answer to this is I was procrastinating. It is not i have not been learning much. I have learnt a lot of new things professionally but could not get them together to share my learning on the blog.

I was surfing the web to dig deep into Java, When i came across this great post by Curious Schemer.

Mr Ray deliberately explains things in the post that we are rhetorically asked to use ArrayList over Vector for better performance .
If we need to make it thread safe. We do it this way :
List list = Collections.synchronizedList(new ArrayList());
But it does not solves the purpose. The thread safety breaks if we do something like this :

final List list = Collections.synchronizedList(new ArrayList());
final int nThreads = 1;
ExecutorService es = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
  es.execute(new Runnable() {
  public void run() {
  while(true) {
  try {
  } catch(IndexOutOfBoundsException ioobe) {

As long nThreads is 1, everything runs just fine. However, increase the number of nThreads to 2, and you start getting this:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.RangeCheck(Unknown Source)

at java.util.ArrayList.remove(Unknown Source)

at java.util.Collections$SynchronizedList.remove(Unknown Source)

Now the workaround to get away with this problem , is to use block level synchronization . Synchronize the methods which update the list to the fine grain level is the best approach to avoid such exceptions .

synchronized (list) {

I learnt my lesson here , one should use atomic level synchrozation to keep the performance and effeciency intact.

The Full Post by Mr.Ray is worth read it explains further conflicts in using synchronization .

Java, LISP, Python

Lisp,Python and Java

While acting on my new year resolutions, I started reading the SICP book .
The book focuses on the aspects of computer programming fundamentals and making the reader learn this by problem solving through LISP  programming language.

So in the first chapter i had to solve some problems , and one of them was :

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

So my implementation of this problem in LISP is here :

(defun square(x) (* x x) )
(defun sum-of-square(x y) (+ (square x) (square y)))
(defun grt(a b) (if(> a b) a b))
(defun grt-2-sum-of-square (a b c )
(sum-of-square (grt a b) (grt b c) )
(grt-2-sum-of-square 1 2 3)

I implemented the same problem with python as following :

#Returns Square
def square(x):
return x*x
#Returns Sum of the Squares
def sum_of_square(x,y):
return square(x)+square(y)
#Returns greater number
def grt(a,b):
if (a>b):
return a
return b
#Returns the sum fo square of greater numbers
def grt_2_sum_of_square(x,y,z):
return sum_of_square(a,b)
# If main mein module is called
if __name__=="__main__":
#print result
print grt_2_sum_of_square(1,2,3)

and implementation of same problem in Java goes like this :

public class Test {
// Returns the Square
public int square(int x){
return x*x;
// Returns sum of the Squares
public int sum_of_square(int x,int y){
return square(x)+square(y);
//Returns greater number
public int grt(int x,int y){
return (x>y)?x:y;
//Return the sum fo square of greater numbers
public int grt_2_sum_of_square(int x,int y,int z){
int a=grt(x,y);
int b=grt(y,z);
return sum_of_square(a,b);
// Main Method
public static void main(String a[]){
//instantiates class
Test t=new Test();
int res=t.grt_2_sum_of_square(1, 2, 3);

There are easier ways of implementing it in python and Java though . But i preferred these as i implemented the same LISP construct. I am amused by the simplicity and cleanliness of LISP code here and no. of lines i had to write for doing this.

I wish i should have been taught LISP in school and i would have been a better programmer :)

Python gives me pleasure of code elegance and simplicity, but Java is the one i am working with . The one that got me a Job.

Learning new things make me feel better, that there is no end to learning.

Powered by ScribeFire.

display tags, Java, Open Source

Exploring Display Tags

Being a J2EE developer you have an edge over others , as you can explore thousands of prebuilt open source components over internet . Display Tag is one cool component which i explored recently .

It provides pagination to the JSP page , one just have to set the arraylist of bean object int the request scope of the page and display tags iterate that arraylist and provides a cool tabular view of the data. One just have to mention property names of beans in the dispaly column tag. It uses commons beanutils for iterating the beans.

Now my point in exploring is that i had to provide much more functionalty for my project .

1. Sorting by table headers
2. Exporting to CSV , Excel file .

Both had a simple solution i just had to mention “export=true” in the table header for exporting the table data.

I had to write “sortable=true” in the table column.

Sorting worked out fine with String values but , there was error for date column as it took date as string also. So i had explore more .

What i did, I changed the type of my date field in bean to java.util.Date . The sorting problem was resolved , But the date format caused problem as i had to show it in GMT . For that I downloaded the source code of display tag from sourceforge. There is org.displaytag.sample package in the source . I placed the source of this package in my code and added decorator to the column :

Now it displayed the proper output.

Now with export set to true in table header . It display links to export in CSV,EXCEl,PDF,RTF formats etc. for exporting to be prper ihad to add ‘’ to the classpath of my application .

export.types=csv excel xml pdf rtf

But it still didn’t work it displayed all the records on the next page.

So what i had to do to sort out the problem was to add a filter fior exporting in the web.xml.

Configure the Filter in your web.xml:


And add mappings for urls the filter will intercept, for example:



Thats it . I made my report table specfic to the client needs.
Thanks Display Tags.

Long Live The open Source Revolution.