Git vs. Mercurial

We’re moving from Subversion to Mercurial as our version control system at work, and since I was addicted to the caveats of distributed version control, I was using git-svn to maintain my working copy on top of SVN, but this is no more needed after the move.

After some playing with mercurial I can only say this: Mercurial maybe a more intuitive and user friendly VCS, but I really miss the fine-grained control I used to get from Git, even if that means occasionally shooting yourself in the foot at times.

I know, I know, you probably think I am a sick demented psycho. But I have no problem with that.

My $0.02. *Sigh*,

UPDATE:
I agree

Quick Shots!

- First and foremost, happy eid everyone.

- To whoever has been repeatedly viewing my resume for the last few days, I’d really appreciate knowing what’s so interesting to you about it?

- You can obviously see the enoromous writer’s block problem I am having in the lines above. I have a lot to say, but obviously I am failing miserably at articulating it. If you have any suggestions about that, please comment!

My Newest Musical Obsession

It’s no secret to those who know me that I am an electronica junkie, especially towards IDM/Ambient/Experimental types of electronica. I’ve happened to be introduced to Rena Jones’s music through SomaFM, and man did I become obsessed! I just love how she blends classical violin with synthetic electronic sounds and glitchy beats.
Here is her newest album “Indra’s Web”:

“Driftwood”

“Transmigration”

Google Chromium on Linux … First Impressions

Perhaps you’ll first say ‘You probably mean Chrome, don’t you?’. Well, not really. Chromium is the open source project behind Chrome (Much like OpenOffice.org to Sun StarOffice, or Fedora to RedHat Enterprise Linux). It seems good things are going to come out real soon, especially with the upcoming release of Google Chrome OS, it seems Google is paying more attention to get Chrome finally released on Linux. Here are my first impressions:

1 – Running it is so smooth, not a memory hog like Firefox.
2 – The V8 JavaScript engine seems to make browsing a lot faster, AJAX interfaces almost run as fast as a native desktop application
3 – On the downside, it doesn’t look anywhere near as cool as Firefox, and not even as cool as it looks on Windows.

To Understand Recursion…You Need To Understand Recursion (Take 2)

Look here first.

So after some insights from Nicolas, I’ve modified my implementations to overcome some glitches. Now this C++ implementation doesn’t wrap around with large values of n. The python implementation also now overcomes the non-deterministic call order it suffered from in the past (credit to the tip goes to my colleague Abdelrahman Hussein).

So here is the C++ implementation:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;

long double fib(unsigned int n) {
	static map<unsigned int, long double> memo;
	if (n < memo.size()) {
		return memo[n];
	} else if (n <= 1) {
		memo[0] = 1;
		memo[1] = 1;
		return memo[n];
	} else {
		long double x = fib(n-1) + fib(n-2);
		memo[n] = x;
		return x;
	}
}

int main(int argc, char **argv) {
	unsigned int n = 0;
	long double x = 0.0;
	time_t t1, t2;
	if (argc != 2) {
		cout << "Usage: fib <n>, where <n> is the input in f(n) = f(n-1) + f(n-2)" << endl;
		return 1;
	}

	n = atoi(argv[1]);

	t1 = time(NULL);
	x = fib(n);
	t2 = time(NULL);

	cout << "Time taken: " << t2 - t1 << endl;
	cout << "fib(n) = " << fixed << x << endl;
	return 0;
}

and here is the python implementation:

def fib(n, memo=None):
	if memo == None:
		memo = dict()
	if n in memo:
		return memo[n]
	elif n == 0 or n == 1:
		memo[n] = 1
	else:
		memo[n] = fib(n-1,memo) + fib(n-2,memo)
	return memo[n]

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print "Usage: fib <n>, where <n> is the input to f(n) = f(n-1) + f(n-2)"

	n = int(sys.argv[1])
	t1 = time.time()
	x = fib(n)
	t2 = time.time()

	print "Time Taken: ", t2 - t1
	print "fib(n) =",x

Running with the same scenarios, the python and the C++ implementations now probably perform as fast (although with this implementation I couldn’t get accurate numbers for the performance time of the C++ program), however I still have got some pet-peeves with Python:
1- The C++ reaches the maximum size of the table when n~=23600, while python segfaults at n=8585
2- I don’t like the fact that I have to explicitly set the maximum recursion depth in python, but rather it should -like Java for example- just throw an exception in the event of a stack overflow rather than at a pre-set depth.
3- I also don’t like that we had to pass the map as an argument to emulate the behaviour of static variables, I don’t understand the rationale behind the absence of such construct in python.

To Understand Recursion You Need To Understand Recursion…

After seeing this problem, and being a developer gradually falling in love with Python (or falling in love-hate with it to be precise). It occured to me “well, why not try to see what python could do?”

Of course, I didn’t want to use the straightforward recursive solution, since as my colleague, Ahmed Soliman, rightly concluded that recursion in Python is slow. (An interesting counter-argument can be found here as well).

To cut things short, I wrote a C++ and a Python implementation of the Fibonacci algorithm with memoization, and seemingly I had some interesting observations

Here is the C++ implementation:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;

unsigned long long fib(unsigned int n) {
	static map<unsigned long long, unsigned int> memo;
	if (n < memo.size()) {
		return memo[n];
	} else if (n <= 1) {
		memo[0] = 1;
		memo[1] = 1;
		return memo[n];
	} else {
		unsigned long long x = fib(n-1) + fib(n-2);
		memo[n] = x;
		return x;
	}
}

int main(int argc, char **argv) {
	unsigned int n = 0;
	unsigned long long x = 0;
	time_t t1, t2;
	if (argc != 2) {
		cout << "Usage: fib , where  is the input in f(n) = f(n-1) + f(n-2)" << endl;
		return 1;
	}

	n = atoi(argv[1]);

	t1 = time(NULL);
	x = fib(n);
	t2 = time(NULL);

	cout << "Time taken: " << t2 - t1 << endl;
	cout << "fib(n)=" << x << endl;
	return 0;
}

Here are the inputs and outputs, note that some values might be incorrect due to overflows, but what matters here is the function call mechanism:

mohd@mohd-laptop:~$ ./fib_memo 40
Time taken: 0
fib(n)=165580141
mohd@mohd-laptop:~$ ./fib_memo 100
Time taken: 0
fib(n)=126979422405
mohd@mohd-laptop:~$ ./fib_memo 1000
Time taken: 0
fib(n)=2071492649197
mohd@mohd-laptop:~$ ./fib_memo 900
Time taken: 0
fib(n)=1862700660921

Now here is the python implementation:

memo = dict() # I don't like this, can't python just have static variables?

def fib(n):
	if memo.has_key(n):
		return memo[n]
	elif n == 0 or n == 1:
		memo[n] = 1
	else:
		memo[n] = fib(n-1) + fib(n-2)
	return memo[n]

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print "Usage: fib , where  is the input to f(n) = f(n-1) + f(n-2)"

	n = int(sys.argv[1])
	t1 = time.time()
	x = fib(n)
	t2 = time.time()

	print "Time Taken: ", t2 - t1
	print "fib(n)=", x

And here are my tests:

mohd@mohd-laptop:~$ python fib_memo.py 40
Time Taken:  0.000197887420654
fib(n)= 165580141
mohd@mohd-laptop:~$ python fib_memo.py 100
Time Taken:  0.00048303604126
fib(n)= 573147844013817084101
mohd@mohd-laptop:~$ python fib_memo.py 900
Time Taken:  0.00656604766846
fib(n)= 88793027306605937532517516910637647045239090036365766884466525589158360259770006891772711976920559280382807770394537471560061517120086971996377683290300054868066659454250625417891167369401
mohd@mohd-laptop:~$ python fib_memo.py 1000
File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
....
File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
RuntimeError: maximum recursion depth exceeded


So WTF? The recursion depth limit is somewhere near a 1000 function calls? To confirm my doubts I cracked open iPython with this scenario:

mohd@mohd-laptop:~$ ipython
/var/lib/python-support/python2.6/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated
  from sets import Set
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from fib_memo import *

In [2]: fib(1000)

RuntimeError: maximum recursion depth exceeded

In [3]: fib(900)
Out[3]: 88793027306605937532517516910637647045239090036365766884466525589158360259770006891772711976920559280382807770394537471560061517120086971996377683290300054868066659454250625417891167369401L

In [4]: fib(1000)
Out[4]: 70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501L

Now python is happy because it has to do only 100 recursive calls to get fib(1000), which clearly shows that the recursion limit is much smaller than what’s offered by the C++ implementation, and even worse, it means that the function behaviour can change according to the order it receives arguments.

On the other hand, the C++ implementation suffers from the fact that lead to wrong return if they exceed the “long long” limit (how many bits is that on x86_64?), but at least the function behaviour is deterministic, and probably the only limiting factor to calling the function is when the size of the map grows too large to be held in the process’s address space hence causing a segfault.

Bottomline, yes, you can theoretically use recursion in Python, but neverthless you should worry A LOT about performance, and even if you find optimizations, be aware of how your functions can get called and that inputs don’t cause the function to exceed the recursion limits. In other words, just spare yourself the time and don’t use recursion in Python unless ABSOLUTELY necessary, othereise Python offers a lot of tricks to get your things done.

Quran Through a Different Eye

One of the most faith-nourishing experiences is to read about one’s faith through the eyes of people who don’t share his faith, yet are sharing their thoughts in an open mind and an unbiased manner.

One of these is the Quran read-along on Jay Solomon’s blog. Of course I wouldn’t agree on every single view, yet it’s a very interesting read.

Enjoy some Quran read-along.

Dear Government of the Arab Republic of Egypt…

… please spare me all the talk about E-Government.

Half of the time wasted in governmental institutions can just be solved by a desktop with a web browser, and lame server with a lamer web app, or wait, maybe even just an Access DB, really, no more than that!!!!!

Thanks!

It’s been a busy weekend…

…2 movies, 2 meetings, and a lot of promising things to come.

So it’s been a long time since the last time I blogged. But nothing is better than having a busy weekend doing fun stuff – whatever your definition of ‘fun’ is- after a busy week at work.

Now to the movies. In 2 consecutive days I go to the cinema to watch ‘El Fara7′ and ‘Teer Enta’, and needless to say that I am impressed. Especially after seeing plotless movies like the unfortunately well-made ‘Ibrahim El-Abyad’, and the unbelievably silly ‘Bobbos’ which marked the first time I ever walked out of a movie.

‘El-Fara7′ (The Wedding) is an intensely dramatic movie about a man who throws a fake wedding in order to collect ‘El No’ta’ (and if you’re not Egyptian, it’s the habit of people giving monetary gifts to newly weds or the parents of a new-born baby) so that he can buy himself a microbus in order to support his family. Now, I don’t want to spoil the movie, but it features a quite nice collection of characters with each having their own problems and goals that they want to solve through that fake wedding. The movie features a nicely woven, complex plot that’ll keep you enjoying every minute of it, which is something unusual in the recent wave of Egyptian movies. However, what I didn’t like about the movie is the over-simplified ending, which could’ve had a great potential of spawning other interesting sub-plots, but all in all, it’s a great movie to watch and a definite recommendation.

‘Teer Enta’ (Go Fly!) is a light comedy and a local adaptation for the American movie ‘Bedazzled‘, the main difference lies in the theme, where ‘Bedazzled’ features the devil incarnated in the seductive persona of Elizabeth Hurley, and concentrates on the idea that seeling one’s self to the devil doesn’t grant you your desires,
‘Teer Enta’ features a funny, peaceful jinny, perfomed by Maged El-Kedwany, and the main theme stresses on self-assertiveness and that one should not put on masks to gain people’s approval, but rather look for those who approve him/her for what they are. The movie is definitely not intellectually engaging (and it doesn’t pretend to, unlike others) but it’s defintely a good laugh, and a good thing to watch, and needless to say that Ahmed Mekki is a real master of the art of parody, something which you don’t find much in the Egyptian movies.

And now to the meetings. Yesterday I have been delighted to participate in Ubuntu Egypt LoCo gathering, we were only a few in a casual setting in one of downtown’s cafes, and we had some quite nice discussions about what activities we want to perform and the outreach efforts we want to do, especially in universities since most of us were actually students. It also included a nice assortment of geek talk and coding-fu bragging :P .

And not so faraway from above, the second meeting was a preparation for some sessions that are going to be held in the Faculty of Computers and Information, Cairo University. We’re going to establish the Cairo Univeristy Open Source Club, where we intend to increase FOSS awareness within the students, especially the CS and Engineering majors. It’ll start off by some basic awareness introductory sessions on what’s FOSS, why is it important, and how you can start contributing. It’s expected to be held on August 1st, FCI Seminar Room, at 10:00 AM, so if you’re interested, you’re invited. See you there ;)

Did you know?

Three questions:

1- Where does this leave us?

2- Where the hell is humanity going?

3- Is it really , and I mean really, good for humanity in the long term?

Comments? (We give free cookies :P )