Monday, July 19, 2010

A Python mystery

Back after a long time…saw something strange today and think it deserves a post. I was cranking through Problem 47 on Project Euler. As I was optimizing the solution, the optimization actually increased run time – and I’m at a loss to explain it. So here goes:

[sourcecode language="python"]
def problem_47(maxlen = 4):
found = False
i = 2*3*5*7 + 1
while not found:
# facs = [len([j for j in uniq(prime_fac(i+k))]) for k in xrange(0,maxlen)]
d = 4
for t in range(i+3, i-1 , -1):
k = len(list(uniq(prime_fac(t))))
if k < 4:
i = t + 1
break
else:
d -= 1
if d ==0:
found = True
print list(xrange(i, i + maxlen))
# if i % 1000 == 0:
# print i
[/sourcecode]

The run time is about 1m2s.

Now, if I try to optimize it such that I break (line 10) when I find the first number from the end that has less than 4 prime factors, the run time should be lesser (or at least the same). Right?

Turns out Wrong… now the thing takes more than 3m to run. What is going wrong?

If any of you have a clue, drop me a comment.

Friday, May 28, 2010

Fun with python’s decorators

Was in need of a utility function that can retry an arbitrary function a few times before giving up. Essentially something like Gmail or Google Readers behavior when there’s no network connection.

Thought it would be a few minutes job to cook up a decorator utility in Python. Boy! was I wrong! I mean, the basic use case is definitely trivially easy with Python – however, once you want something that’s more useful than that and resembles something that you’d actually use in production, the complexity goes over the top!

Anyway, I’m figuring out all sorts of fun things about decorators – and all of it the hard way! OTOH, its a  lot of fun to write small test code to test & validate assumptions!

Make no mistake – I’m still a python fanboy :) – just that going through some pains with decorators right now. Will follow this up with a longer/detailed post that may have some useful insights I’ve gained till then. Thanks for stopping by!

Saturday, March 06, 2010

Back after a long time…

Obviously, I’m not writing enough out here… part of the reason being even though wordpress’s web editor is great, I really like not having to type gobs of text in a text area.

So eventually, looked around and found Windows Live Writer. Its going out on its customary spin :).

So what’s been cooking? Actually a bunch of things over the last several months:

Stuff – on which I mean to put up individual posts

  1. Had a fun exercise benchmarking lighttpd with python wsgi
  2. Been doing some stuff on mysql cluster – mostly around seeing how it compares with the mysql master-master replication setup I had in place.
  3. Dipping my toes into Amazon EC2 finally – though Linode or Rackspace is way easier if you want to just spin up a VM. Amazon’s  EC2 does have some interesting stuff (reliability of back ups, CDN etc). However, it comes at the cost of having a model that initially is hard to understand.
  4. Resin server – heard good things about it, had to see if it would fit at  some stuff at work. Disappointed that the free version is really hamstrung.
  5. Apache Wicket: I’ve always hated web UI and somehow the action oriented frameworks (Struts and their ilk) never appealed from a coupling/cohesion standpoint. In that respect, seemed like ASP.net got a lot of things right going the component oriented way. However, it seems fatally flawed with stuff like viewstate and postback and so on. On the Java end, tried Tapestry out, but, it comes with too much baggage for my taste. Had been reading of Wicket for sometime now and decided to take the plunge and was pleasantly surprised doing my contrived example:
    1. Took much less to get off the ground compared to Tapestry
    2. Mentally, a lot easier to understand
    3. Managed to realize my goal of exploiting OO techniques to DWIM – even on a simple contrived example.

Books:

  1. Steve Souders excellent "High performance websites” book: if you’re doing anything near a high performance website, then grab this book today!
  2. Wicket In Action
  3. Agile Principles, Patterns and Practices by Robert C Martin: read about the SOLID principles first and then buy this. This is a book to own if you aspire to become a good Agile/OO practitioner. Don’t worry about the C# in the title – it applies universally.

Saturday, January 30, 2010

A new tool for the toolbox!

Firstly - my VM setup:

I'm running Virtualbox with Xubuntu 9.10 on Win7 host - and its pretty. Its on a office standard issue Dell D531 - meaning they're AMD Turion X2 TL-60 and 2GB of RAM.

Now the Turion's supposed to have hw virtualization (AMD-V) however, the moment hw virtualization was enabled in virtualbox and I tried starting the vm, the machine would hard reboot!!!

After searching high and low, turns out that its an issue with Dell bioses and they dont have any updates. Here's a page that tracks the issue. Imagine my happiness when a couple of days ago, found that dell had released an unofficial bios update (T12).Well, its gone in, and things are running swimmingly well - my VM now has 2 procs, is stable and I hardly feel I'm in a VM :). In fact, this post is coming from the VM  - firefox with 12 tabs, a few terminals and emacs running on 600 MB of RAM.

Now let me come to the new tool I was talking about

I like to run the VM full screen - feels best that way. After trying out enough and more of multiple desktop softwares, have finally settled on VirtuaWin - beats the crap out of other tools, systray integration is great, has window rules and so on. Over the past couple of weeks, its come close to the ideal tool - does the job well and you hardly know its there :-)