Filed under code, java on November 27 | 0 comments
This week I was told that my way of going through each elements in an ArrayList was not the most efficient. I should be using an Iterator.
No reason to doubt an experienced programmer so I changed my code.
I never trusted anyone when I did troubleshooting before. I always need to see the evidence for myself. So I decided to do this code to check the validity of the data I was given:
/**
* Testing the performance of different ways to go through a list of items
*/
package com.cinq.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @author mc
* @version 0.1.0
*
* last updated on 2011-11-27
* initial release of these tests
*/
public class Performance {
/**
* @param args
*/
public static void main(String[] args) {
long dstart = 0;
long dend = 0;
// Allocate an ArrayList
ArrayList<String> data1 = new ArrayList<String>();
ArrayList<String> data2 = new ArrayList<String>();
for ( int i = 0; i < 10000; i++ ) {
data1.add("abc" + String.valueOf(i));
data2.add("xyz" + String.valueOf(i));
}
// Start going through all the elements with a get index method
dstart = System.currentTimeMillis();
for ( int j = 0; j < data1.size(); j++ ) {
data1.get(j);
for ( int k = 0 ; k < data2.size(); k++) {
data2.get(k);
}
}
dend = System.currentTimeMillis();
System.out.println("Get Index for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");
dstart = System.currentTimeMillis();
for ( String s1 : data1 ) {
for ( String s2 : data2 ) {
// nothing
}
}
dend = System.currentTimeMillis();
System.out.println("For each for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");
dstart = System.currentTimeMillis();
Iterator<String> it1 = data1.iterator();
try {
while ( it1.next() != null ) {
try {
Iterator<String> it2 = data2.iterator();
while ( it2.next() != null ) {
//nothing
}
} catch ( NoSuchElementException e0 ) {
//ignore
}
}
} catch ( NoSuchElementException e1 ) {
// ignore
}
dend = System.currentTimeMillis();
System.out.println("Iterator for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");
}
}
The output I get from this is:
Get Index for 10000 by 10000 elements took 12 ms.
For each for 10000 by 10000 elements took 271 ms.
Iterator for 10000 by 10000 elements took 166 ms.
This makes no sense. I should see the performance of the Iterator way of doing things to be faster than my get(index) way. I will have to ask my co-worker tomorrow why I am getting this bad performance. Conclusion to follow in the next few days.
Filed under code, java on October 28 | 0 comments
Sometimes you have this basic question that you are too shy to ask around because you should know. Reality is that I always have a few of those and eventually you have to look around for what others are doing if you want to get better.
I just completed some code and I was wondering if using the return code for errors was a good idea or if I should have used exceptions everywhere.
After reading a few articles I think that I did the right thing.
One key sentence that I like is: exceptions are for exceptional conditions. So you should see an exception if the failure is not expected and abnormal to the working of your application. A good point.
Others are saying that you should crash loudly. They are saying to use exceptions as much as possible so that it gets your attention and you fix the issue sooner than later.
The argument that return code makes the application more stable seems to be a bit simplistic because you might be hiding issues that will be more difficult to identify later.
There is room for both approach and I think that experience tells you what to do. Always using one or the other does not seem to make sense. Balance.
Return code makes sense right now because this Java code needs to integrate with some Bash code. If the solution was purely Java I think that I would need to change this strategy.
I may start using more checked exceptions since it gives me a better sense of balance and allows to have more information about the failure.
Continuous improvements.
Filed under java on October 20 | 0 comments
I was searching on how to use some library in my Java code and I came across the GrepCode.com site.
It was great help to find the documentation/code for the library I was using. They only claim to have index for Java, JBoss and Eclipse but the F5 library was there as well.
I installed the Eclipse plugin because that could save some time when searching why I am getting a certain error or why some code is not working as expected.
You find new things every day.
Filed under java on October 20 | 0 comments
I have not started to use Java 7 but I have started to read on the differences. This is a great blog post to learn a few new tricks to optimize the JVM:
http://marxsoftware.blogspot.com/2011/10/javaone-2011-definitive-set-of-hotspot.html
It is also funny that the small, almost irrelevant, option is the one that will relieve a serious pain for me:
-XX:+PrintGCDataStamps If this works as I expect we will be able to read GC logs and easily figure out when events have happened
Filed under code, java on September 23 | 0 comments
Wow.
This new type of debugger is something on its own.
It allows you to record your application executing and then replaying it has you need to figure out where the errors are. You simply need to watch their video to get the “wow” factor right away.
It simplifies the code you have to do and gives you a better insight in your application.
I am quite curious to know how it would work on JBoss servers running multiple apps.
http://www.chrononsystems.com/
Filed under code, java on January 6 | 0 comments
I am in the middle of adding features to a web application that uses the Spring Framework and since I have not had any training I am learning as we go. As much as I know better this is where I am anyway.
I have been fighting for a few days with a java.lang.NullPointer issue and it was driving me crazy. I knew that it was some small details that was escaping me but I just could not put my finger on it.
I got a breakthrough yesterday evening by re-reading a form post about the fact that you can’t use the new operator on objects that are wired by the Spring Framework. I was also missing the getter and setter method for that entity and then everything started to work. Small victory that certainly pleased me because it was starting to get to my self esteem.
I was a bit mad at myself for not understanding the tip that was given in the forum on the first read. I was looking for something else and the truth was starring me so obviously. The madness will certainly make sure I remember to pay attention to every recommendation experienced people make when answering other beginners like me.
Understanding how the framework does its things is starting to sink in and I am starting to see the full potential this framework has to quickly develop web application.
If I can just spend some time to read on it and understand all the libraries and a few more founding concepts I could stop wasting my time fighting with it.
Filed under code, java on January 4 | 0 comments
The trivial issues are the one that drive you nuts because you waste too much time for something that should have taken 2 minutes at most.
Today I was fighting with a spring application that uses hibernate to talk to an embedded derby database.
So simple.
Because I am getting a java null pointer exception when I am trying to save an entity I decided to empty the embedded derby database directory so I could start fresh and make sure that this was not the issue. Again simple.
I started to have all sorts of issues in my spring application logs telling me that it could not create the database (I have the property for derby set to create=true).
All I needed to do was to the delete the directory itself and not just the content.
An hour wasted searching on Google and no one was as dumb as I and posted anything about this issue.
Filed under code, java on July 23 | 0 comments
I spent a little bit of time to figure out how to find the jdk version used to compile a java class. On the site stackoverflow they have a great example of java code that can get this information. Unfortunately that did not resolve my problem so I will have to hunt down a bit more on why I get this:
Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Filed under java, Linux on December 30 | 0 comments
That was not an obvious one. Under KUbuntu I typically use the KPackageKit application to install and update applications. It did not work well. I tried to install the sun-java-jdk package many times and every time it would list the dependencies and do the install but the “java -version” would tell me that I was still using the OpenJDK.
After reading a few different posts in forums I tested the idea to use the synaptic package manager. The installation worked much better.
To finalize the install I ran a few commands at the cli:
sudo update-java-alternatives -l
This listed the fact that I now have the OpenJDK and Sun Java installed on my workstation.
sudo update-alternatives –config java
This allowed me to specify to use the Sun Java as my default JVM.
java -version
This now reports the Sun Java JRE as my JVM.
I can now use Maven to compile some test projects.
I guess there are still a few issues with the OpenJDK to be fully compatible (might just be some configuration that I am not aware of).
Filed under code, java, software on September 3 | 0 comments
This is not the first time I do this and it is on the edge of embarrassing except this time I knew I had tools to get me out: lose the source code to a java application.
I have this application that was done a year ago and I guess that in the rush to get it to production I forgot to import the code in SVN.
A year later we have changed a few things and now I have to change a few lines in the code and add a method to it.
I remembered using a command line tool under Linux to decompile but since I could not remember the name of it I had to search for it. In the first few results of my google search for “java decompiler” came this site:
http://java.decompiler.free.fr/
I tried to install the JD-Eclipse but I don’t see how to use it so I simply downloaded and used the JD-Gui for Linux and it works beautifully. I can see my source code in the .class file in 2 seconds and now I can easily change it. This is so convenient.