Saturday, May 2, 2009

Python vs. C#

Just for lazy programmers who try to change the reality to fit their laziness, from now on, I will put some sample codes comparing Python and C#. Lazy developers usually claim about the existence of dynamic languages and new programming concepts, without considering their success during these years and even facts that even Micro$oft tries to support them more and more. DLR an F#, and even declarative parts of new C# proves that.
This is a simple binary serialization of a simple dictionary in both languages. We need an array of bytes containing serialized data (e.g. to put in a RDBMS field). After that, I added a line to tell us the length of stream. After comparing 'lines of code' and complexity, you can even compare the 'Binary' size in C# and Python:

C# Code:

using System;

using System.Collections.Generic;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

namespace TestDicPack

{

class Program

{

static void Main(string[] args)

{

MemoryStream mem = new MemoryStream();

Dictionary<string, int> dic = new Dictionary<string, int>();

dic.Add("a", 2);

dic.Add("b", 3);

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(mem, dic);

byte[] result = mem.ToArray();

Console.WriteLine(result.Length);

Console.ReadLine();

}

}

}

Result:

1380

Python Code:

import pickle

dic={'a':2,'b':3}

result=pickle.dumps(dic,2)

print len(result)


Result:

22

6 comments:

  1. 1 - IMHO this is not a proper comparision. You could add an Extension Method named 'Dumps' to type Dictionary<T1,T2> and just write myDic.Dumps(). Because as far as I can understand from python 'dumps' is a helper function (like a static method) in some kind of package named pickle. So I (even) prefer C# 3.0 version which is some kind of type expansion (mixin). Can we provide something like 'dic.dumps' for above code (with no reflection and just using language tools)?

    2 - You should have an eye on C# 4.0! It added dynamic blocks! so you can write code like:
    static
    {
    ...
    dynamic
    {
    ...
    }
    ...
    }
    And in dynamic part everything is late bounded!

    ReplyDelete
  2. 1. dumps is not a helper function. It is a built-in function and you can use it from older Python versions. You may write an extension function but you will have to write more lines of code than Python version.

    2. Adding dynamic parts to C# proves my idea about that Dynamic is good!

    3. If C# is able to do that in version 4, Python has been able from 2 (Years ago). You must compare C# 3 (Released) vs. Python 3.1 (Released) not Python 2.3+ (in this article). This is not just to compare a beta version (C# 4) with a released old version (2.3).

    ReplyDelete
  3. 1 - By helper function I have not meant that It is not built in but it is not OO and it is more like bunch of global functions piled in a utility class. So it is a bad designed library.

    2 - I am not insulting Python pal! I am saying I prefer the "Statically typed where possible, Dynamically typed where needed" approach of C# 4.0 and VB.NET than a totally dynamic world like Python because I think as my code base get bigger, there will be harder times to manage a big bunch of code.
    And this approach is new for C# but not VB.NET. VB.NET has it from very early versions. It is just I do not like syntax of VB.NET (as I do not like syntax of Python too)!

    ReplyDelete
  4. I think, C# vs. Python is something like Los Angeles Lakers (NBA team) vs. Barcelona FC! I started learning Python few days ago and know C# a bit and I think they're completely different! Python is great, C# too. Python is crazy in calculation. C# is fine in implementing. But I'm agree with you in LOC. Python is wonderful! :)

    ReplyDelete
  5. VB.NET syntax is very different to Python and any other dynamic typing. I have been a VB developer for years (since VB1 in DOS!). VB does not support dynamic typing. It just lets u use a variable u have not declared. It is not Python. See my new post here.

    ReplyDelete
  6. VB.NET has a very different syntax to Python indeed. It is a matter of taste and I have already made it a personal preference.

    VB.NET allows you to use late binding too. But not constructing a type dynamically.

    ReplyDelete