“Before you speak, listen. Before you write, think. Before you spend, earn. Before you invest, investigate. Before you criticize, wait. Before you pray, forgive. Before you quit, try. Before you retire, save. Before you die, give.”

Thursday, April 7, 2011

Problem 14 Collatz Ruby vs. Java

Finally after joining the Euler club I have solved the Collatz problem. As it turns out this problem cannot be proven mathematically .. and some mathematicians went nuts trying to prove it.
For me it is intuitively simple .. you divide more than you multiply and that's that.
Anyway I have spent almost 3h on this problem 20m writing a solution in Ruby and ... 2h 30m running it.
On my home machine I have a Ruby 1.8.7 on Windows 7 environment and my computer a Core i7 860 8GB RAM machine (good enough I would say). I gave up at around 1 AM while it was half done.
Here is the code.

def chain_collatz(n,cache)
chain_length=1
puts "-> #{n}"
if n==1
return chain_length

else
if cache[n]!=nil
puts "cache_hit"
chain_length+=cache[n]
return chain_length
end

if n%2==0
n=n/2
else
n=3*n+1
end

chain_length+=chain_collatz(n,cache)
cache[n]=chain_length
return chain_length
end


end

#puts chain_collatz(10100,cache)
#puts "cache"
#puts cache.keys
cache=Hash.new
def get_number(n,cache)
max_chain_length=1
vnumber=13
max_number=1
for i in 13..n
chain_length=chain_collatz(i,cache)
max_number=i if chain_length>max_chain_length
max_chain_length = chain_length if chain_length > max_chain_length

number=i
puts "Done with number #{number}"
end

return max_number
end

puts get_number 1000000,cache


After a lot of pain it ran and gave the correct result. Found it only this morning.
After having the problem solved I rewrote it in Java to see whether I could have it any faster:
The result?

import java.util.HashMap;
import java.util.Map;

public class Collatz {
public static void main(String args[]) {
Map m = new HashMap();
Collatz collatz = new Collatz();
long startTime =System.currentTimeMillis();
System.out.println(collatz.getMaxCollatzTo(1000000l, m));
long endTime =System.currentTimeMillis();
System.out.println (" Execution took" +(endTime-startTime) +" milisseconds");



}

public Long chainCollatz(Long number, Map map) {
Long chainLength = 1l;
if (number == 1) {
return 1l;
} else {
if (map.containsKey(number)) {
// System.out.println("Cache hit" + number);
chainLength += map.get(number);
return chainLength;
}
if (number % 2 == 0) {
number = number / 2;
} else {
number = 3 * number + 1;
}
chainLength += chainCollatz(number, map);
map.put(number, chainLength);
return chainLength;
}

}

public Long getMaxCollatzTo(Long number, Map map) {
Long maxChainLength = 1l;
Long maxChainForNumber = 1l;
for (Long i = 13l; i < number; i++) {
Long chainLength = chainCollatz(i, map);
if (chainLength > maxChainLength) {
maxChainLength = chainLength;
maxChainForNumber = i;
}
// System.out.println("Done with "+i);
}
return maxChainForNumber;
}

}


The Java code took only 50 seconds to run.

Anyway puzzled by this I started optimizing the code in both Ruby and Java .. removing all but essential console outputs.
Finally I got to test it on a Linux OS with similar processing power (Ruby implementation in Windows really sucks I think).
Final result.
Ruby 1.8.7 11.7 seconds
Java 6(1.6.0.22) 1.471 seconds
Tested also with ruby 1.9 and got it down to 3 seconds .
This puts Java on top of my list for Project Euler problems and makes me question the ability of Ruby to run unoptimized code.

But still for prototyping it's great.


Saturday, March 26, 2011

"Limitless" and real life

Last Sunday I went to "Limitless" (trailer here) and enjoyed the movie.
Would I like to be in the "zone" all the time ? Completely focused ? Very calm ? see "50 moves" ahead of everybody else ? Win big in the stock market by making accurate predictions ?
Just take a pill :). The only issue ? It will kill you in a few months.
The hero in the movie is enhanced by this pill and finds a solution to create this pill without side effects before the pill kills him. This is what I call a smart investment :).
If only it were that simple ...
Unfortunately it is not that easy in the real world but as well as financial investments (which take time and intelligence as well and know nothing of ;) ) there are educational investments. Things that you learn that can help you learn more things and advance you ..
I recently joined a site called ProjectEuler that is basically giving you some problems (more complicated as you go) and you get to several levels. I am at level 0 with only 8 problems solved but I find the challenges pretty interesting.
Of course you need to write code to solve those issues and I mostly use Ruby .. because it's faster. How long would it take to solve the problems without a computer ? I really don't know and I think just having the "Limitless" pill can tell. Of course you understand the algorithm but your computation skills suck.
So the "Limitless" pill is in my opinion a fast computer and a good programming language you can use to communicate to it (Ruby,Python.Java .. whichever you feel comfortable with). Learning and using A.I. techniques can help your limited algorithm knowledge.


For instance you can solve the famous NP complete problem : traveling salesman path with genetic algoritms (Here is a solution) .




So the computer can find solutions to what your mind can't effectively find.I thought that in order to solve this problem you needed discrete mathemathics knowledge, graph algorithms knowledge and of course complexity evaluation knowledge.
Well you can't actually use it because it's a NP complete problem .. that is it's not possible to actually prove it mathematically.
















Last but not least while researching some AI stuff I came across a knowledge tool I like
It's called Wolfram and it is great for finding scientific information about stuff. I really liked it and I think it would be nice to integrate with a commercial site. The applications can be amazing.
Still I want to point out something else. I tried working on difficult solutions before while working in an open space environment with eyes set on my screen from all around me. Now I work in a small office that is very quiet. I am starting to find solutions to problems I could never
find solutions before, seeing patterns , cleaning old rusty code.
Once and for all the actual office environment does matter. No great writer ever wrote with other writers in a open-space office. No great programmer/hacker did it in a bullpen 1 feet from the colleagues. Mental focus is critical to problem solving.
Of course when you are coding another J2EE application that needs to run on a 8 year old server with 2004 technology and which will probably be used just for 1 year and does just CRUD it is hard for any actual programmer to justify having his own office , a quiet environment, books or other educational investments. Been there done that.

Saturday, March 19, 2011

Finally a manager I like

As I already mentioned I am a fan of Joel Spolsky and of Peopleware.
I do believe in getting into the zone and developing quality code without interruptions. I jumped ship to a smaller company in order to actually develop code without doing 1 meeting/day.

My ex-manager used to call 2 meetings /day when things were not going as fast as required.
Then one of the members of my team asked : When are we going to work then ? Things are pretty desperate now .. it will go worse with more meetings . Sadly this guy was the first to be sent home when the time came.
Right now I am doing Scrum and I really enjoy the work from home approach to implement the caves and commons pattern .. when I really need to focus and get things done I stay at home.
The weird part is that more and more managers claim that the more you are interrupted the better the work you deliver ... my opinion is that communication time and "zone" or "flow" time must be balanced carefully and more to the zone time for knowledge workers

Jason Fried from 37 signals pretty much nails it with this video:

Sunday, March 13, 2011

Work from home ... and the laptop alternative.

It has been a long time since I have not posted. Between my busy programmer/engineer life , my fitness schedule and my time spent reading about finances/investment I do not find time to log my thoughts.
So this year I joined a new company a small startup (which will remain nameless for the time being).
One of the benefits was a very expensive state of the art Dell Precision laptop equipped with SSD and 8GB RAM .. something I could not afford to buy with my salary as a software developer and the prices in this city.
The other benefit was working from home (no limit there) . As I am obsessed with "the zone" you can imagine that I took that benefit and took a day off .
My vision on laptops was the following : Very expensive equipments that are only effective for the rich guys to check their emails and do some office work. I could buy my Core i7 computer for about 700 Euro .. a laptop would have been 2200 Euros at least.
Once I was given this laptop I found my self working on company's projects (or personal stuff ) whenever I had time. I developed a user interface while waiting for my car's 10.000 km maintenance ... I coded in restaurants and of course from home.
Well there are some drawback normally : like no dual monitor, no good keyboard mouse , no ergonomic position.
Thankfully at work I have a dual monitor setup with a docking station allowing me to plugin whichever keyboard I want.
So I had a blast working at home one friday .. 12h of uninterrupted productivity.
The only drawback : You can't stop and go home ...you're already there.
Of course Skype contacts and email helps .. and the fact that everybody gets to work from home as they please.

Wednesday, January 5, 2011

Seam Framework or the JEE answer to Ruby on Rails

After playing with Ruby on Rails for almost a month (and coding my weight management application) I can honestly say I have found my love for coding again. Mostly because of the instant feedback effect.
As I kept reading various resources on RoR I found one issue with it. It does not support XA transactions. That is if you are using an application that has to synchronize 2 databases in a transaction RoR can't help you.
Of course RoR is just a framework and Ruby standards are not as good as Java ones so there is no JTA (Java Transaction Api) equivalent in Ruby/RoR hence you are on your own. So writing enterprise stuff in Ruby will just have to wait for a while.
That did not stop me from spending the New Year's eve and 2 days after just coding a RoR application between drinking ,eating and celebrating
Now recently I have decided to study Seam Framework as the JEE answer to Java. I have received Dan Allen's "Seam in Action" book and I am trying to make sense of it.
Seam's fantastic points
1)Conversations
2)Bijection
3)Handling JSF actions on initial Request (or get)
4) Using a conversation scoped persistence manager should prevent all the LazyInitializationExceptions I kept seeing in the past 2000 days of JEE programming (not that many but you understand ..)
5)Integration with Drools , jBPM-of course best to be used in a JEE container.
A lot more small stuff that makes your life easy.
Unfortunately I can't help thinking that 2 heavy weight containers (JSF and EJB3) are integrated in a bigger container. The problem is whether Seam provides a good abstraction or a leaky one.

Of couse while reading Dan Allen's book I found that at least in the 2 initial chapters he kept trying to offset Rails .. unfortunately hot deploy is not complete in JBoss/Seam .
There are several severe limitations
  • you need to be using a war project (no EJB's)
  • you need to have a tag specifying debug =true
  • you need to configure the ide to do the explode on edit thing(or type seam explode)
Even then you would not be allowed to do Entity classes changes and changes to Session Beans without redeploying.
So it is true that it helps but even Dan specified in a Seam Framework post that incremental hot deploy in Seam is a bit of a hack .
What I did enjoy was doing integration testing with Seam , TestNG and Embedded JBoss these shoud alleviate the pain of repeated build/deploy/test cycles I mentioned. Nevertheless integration tests are always slower than basic unit tests.
I can run 54 pure unit tests on my machine in 2 seconds but just 4 integration tests take 16 seconds.
For the record my machine is now upgraded : Core i7 860 , 8GB RAM (Corsair) ,1TB HDD so I guess we are talking top of the line performance.

As a side notes I found that doing RoR development using JRuby and Webrick makes testing in the browser faster then integration testing in controllers (yap JVM start times to the rescue) so doing TDD with JRuby is not easy either.Nevertheless they take 2.8 seconds to execute + 10 seconds JVM startup time (that's why Java will never be used for OS development :) ).


Anyway if you need to stick to the JEE world or need distributed transactions go for Seam , if you are confident you won't need distributed transactions you cand certainly go for Ruby/RoR.

My objectives for the year ? Just get to 100 kg and stay there.
What I need to have this :
  1. Diet and calorie counting
  2. fitness performed regularly.
I even wrote my own RoR code to track this (not yet business ready but functional)
Oh btw : Happy new Year.

Sunday, December 5, 2010

Ruby on Rails or the beauty of instant feedback

I was trying to write a GWT application that would help track the weight , calories and training for a person and make it run in the Google App Engine. Unfortunately I was always blocked by some configuration issues which made me delay it (being tired after work does not help).
So Saturday I thought maybe I should give this Ruby On Rails a try since I am into ruby a lot lately.
Using Netbeans I started with the sample "Depot application" and started to understand RoR a week ago.
After watching this video of DHH presenting Ruby on Rails and the concept of scaffolding I was able to get started in no time .
Today (less than 24 h since I started) I have a working weight tracking application which needs some improvements .. but it is working.
Being able to avoid the compile/deploy/test cycle and replacing it with just "test" was a huge benefit for me.
Instant feedback is really enlightening and reminds me of the reasons I actually loved PHP years ago. As a beginner programmer I wrote "working" PHP code that was horrible with no MVC separation .. or layering. It was a shame but it worked.
Right now when looking at Rails I see somehow what PHP should have been like ages ago .. the benefits of PHP and the engineering I used to see only in J2EE frameworks. And best of all I did not have to write one SQL line .. this is fantastic for me :).

Yes I know a Jedi JEE programmer must be able to just figure out things such that a single deploy will be enough for all fixes. Well it usually ain't the case and I find myself waiting for the machine to deploy only to find other things to fix .. which I thought were fixed and the human brain can only keep track of 7 things at a time .. add to this the typical open plan office space , personal concerns or other thoughts and you get minimal productivity ..
I realize running the projects inside Netbeans and/or Eclipse allows sometimes the automatic redeployment but even that takes time.
We have MVC , Active Record pattern for ORM , automatically generated unit-tests, integration-tests, functional tests , the famous convention over configuration rule , clean layers (except the service layer that for now I do not miss).
As I read in Bruce Eckel's "Thinking in Python" (for a while I wanted to choose between Ruby and Python) it's not the static type checking or compiling that brings so many benefits it's the use of unit-tests.
Hence I strongly believe well written Ruby on Rails applications can be equal or equivalent to J2EE applications for the same purpose it the test driven development technique is used. Moreover I wrote way too much Java to know that not using unit tests and a technology like EJB2.0 can lead to almost 0 productivity (unit tests seem to help as you do not have to start the container just for a small function you just wrote).
Yes the same can be said about PHP .. you can write horrible code in any language , but Ruby is the new high level language of the day and I tend to go with the flow.
Of course being an Java developer /architect wanna be I realize my 10 years experience in Java are going down the drain with this Ruby on Rails stuff but if I can get things done while having fun .. why not ?
Of course if I have to develop something that has to be distributed and integrate with other tools I will still go for Java but some smart guy invented JRuby meaning I could be running Ruby in a JEE container in order to do a slow migration.
Of course making a JEE application go through the build deploy cycle without issues would be nice , I just can't see how.
This link shows how much time we are loosing with deployment and 2.5 weeks a year looks like a lot considering just the salary of the average developer.

Sunday, October 24, 2010

GWT compiling performance

Just a reminder for myself of the required performance to compile a GWT -GAE Application.
This is on my Core i7 860 machine with only 2GB of RAM (for now).Some people might notice I am running on 1GB of swap so let's just say that this is the blocking issue for now. Still it's a core i7 running at full power for a while just to do this processing. I wonder if people developing with GWT do this kind of work on Core i7 980 processors (extreme hexa-cores processors).















I have created a Proof of Concept online for a Java application that I want to test but for now it is not quite ready to go public.
Google is number one for offering some things I could not find online
1) Affordable Java web hosting - this one is free for small loads
2) The GWT toolkit saving me from writing Javascript (I don't like Javascript very much).
3) The AI driven car which they are working on (but on this on a different article)-

Still the approach is causing me a lot of headache. You need to be able to write Java code which is then transformed into Javascript and then the whole package is transformed into a war and deployed on the server.
My other home hobby project is created as an ear and it only takes 40 seconds to compile , run unit tests and create an ear.
This one ( a very stupid GWT hello world) took me as much as 2 minutes to do the same thing in a normal non -blocking mode(no other consuming applications) 1 minute 30 seconds.
I need to look into it but I guess generating JavaScript is not quite instantaneous.
The benefit of writing Java code AND unit-tests is clear. You know exactly what goes wrong and why it goes wrong (if you are a decent Java programmer).


Nevertheless I think that adding another abstraction layer on top of JavaScript might cause some impossible to fix bugs ( without JavaScript knowledge).
It took me a lot to just compile classes and make GWT compile a different set of classes .. because GWT classes do not mix with backend classes such as Hibernate or JPA classes so you need to specify that accordingly.

Will it work ? Well I will let you know soon enough as I am preparing my application.

Later Edit : After a week fixing a RichFaces bug involving extending both RichFaces and Facelets frameworks (just to handle a ViewExpiredException and to make IE7 handle a redirect ) I have the feeling that GWT might be a better alternative then the JSF and Javascript combination at least in the short run.
Of course there is always Flex which is a bit safer but involves also learning anew language and buying the FlexBuilder.