Pages

Groovy performance : iterating with closure vs loop

Toady while listening to this video I discovered an interesting fact about usage of closure vs regular loop for iterating over list. So I wrote a quick test to confirm and yes loops outperform closures in terms of performance in case of iterating over a list.

But in a slightly similar case of File.eachLine{} vs loading file in a list and then iterating with for, the closure still works out faster on the whole. I have not tested this for different file sizes, but damn, whatever it may be new File(file).eachLine{} just rocks :)

Here is the github gist, to get the latest version of code.

def file = "input.txt"
// just to heat up the jvm :)
new File(file).eachLine{
    it.toUpperCase()
}

// with closure
start = System.nanoTime()
new File(file).eachLine{
    it.toUpperCase()
}
println System.nanoTime() - start

// with loop
start = System.nanoTime()
lines = new File(file).readLines()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

// read lines into a list
lines = new File(file).readLines()

// with list.each {}
start = System.nanoTime()
lines.each { line->
    line.toUpperCase()
}
println System.nanoTime() - start

// with loop over list
start = System.nanoTime()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

Results (with groovy-1.8.0 and jdk-1.6)
 Run # 1   Run # 2   Run # 3   Run # 4
 4792229   4773791   4840280   5428064
11169576  10254934  10791595  12546008
 2638604   2424610   2477969   2466514
  465702    315403    315403    412063

Display System Properties in Groovy

It can't get any simpler than this.

System.properties.each { k,v->
    println "$k = $v"
}

Groovy rocks!!

Back in the Saddle ... Back in Black

I am back to blogging after a brief ;-) hiatus of 2 years. Don't get mislead by the title of the post. This post has got nothing to do with the two tracks, Aerosmith or ACDC.

It's like a rebirth. Giving a fresh life to a thing I care about. Yesterday I finished reading the fantastic book named "Clutter Busting". It has inspired me lot to clean up the junk, that i dint consider junk so far, from my life. I have started removing many of my precious belonging from my computer attic.

I cant believe its almost been a decade of getting hooked to computers. The Digital Clutter had been gathering for long time that kept me distracted and less focused. You can expect few things coming straight out of my attic making their way to this blog.

Leaking the secret of not posting for so long: I had decided that I will move to a hosted blogging platform (am a control freak, I guess) and own a domain name and only then I'll publish my next post. The sad news is neither of that happened so far and the good news is I have decided to change the way I have programmed myself to think.

By the way today happens to be the day when I started my professional career six years back.

Hello world!

BlogCampPune2

BlogCampPune2 is a blogger unconference. Its happening for the 2nd time in Pune. It is completely free and sounds like serious fun. Instead of looking for more details here, I think it's better if you check out their cool wiki page.

I have registered and I'll be attending this the first time (I'll try my best to attend, unless I get some official issue to solve(slog) on weekend like every other weekend). And I hope those of you who are in Pune would be interested in it too.

Ubuntu 9.04 overheats my notebook

For last few days I observed my notebook (Sony vaio FZ140E) was heating up too much. Also battery could last for only 15 mins to half hour max. I thought it's EOL(End Of Life) phase of my laptop. But then, for some reason, when I booted it in Vista, the problem was not there. A little googling suggested that there is some bug in the ubuntu that is causing this issue. I am not at all aware with all the cpu cycle/frequency related terminologies quoted, but it has got something to do with percentage of processor usage. A lot of people seem to have same complaint. So the culprit is not my notebook :)

I hope Ubuntu folks will come with the fix soon.

Ubuntu upgrade

Today when I turned on my notebook, I realized that its April and the fresh new release of ubuntu should be out. I checked out the site and yeah, it was.

So I upgraded my notebook from 8.10 to 9.04. And it was a breeze. For those who wonder what the version numbers are, its really a very simple logic. The major version number is the year and the minor is month. 9.04 means released in month of April'09. 8.10 meant the version released in Oct'08. Ubuntu usually releases new version in these two months only. Can you see how simplified is the logic behind the version numbers. Doesn't it tell you about how simple this OS is?

If you have not yet checked it out, please do. Its free :)

Disable unused windows services

There are lot of windows services that unnecessarily load on start-up and consume resources. You can find more info about such services here. Disabling these or turning them to manual mode instead of automatic may greatly improve performance and boot speed. I am doing it right now. Thought of sharing with all.