Nguyen Kim Son

Performance test between generics and collection of Objects

In Project Euler on May 14, 2012 at 12:47 pm

Below is the code in C# for testing the performance of 2 approaches in creating a collection: one by using Object, then implicit casting and the other using generics technique.
In the code, we first fill up 2 stacks, one created by collection of objects, the second created using generics. Both contain lot of elements (5E7). We then calculate the sum of all elements. The first one takes 6.3 secs, while the second one takes 1.3 sec. The reason of more than 5 times difference in execution time is due to the overhead of casting.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiverseTests
{
class Program
{
private const double M = 5e7;
public static Stack GetStackB4Generics()
{
Stack s = new Stack();
for (int i = 0; i < M; i++)
{
s.Push(i);
}
return s;
}

public static Stack GetStackAfterGenerics()
{
Stack s = new Stack();
for (int i = 0 ; i < M ; i++)
{
s.Push(i);
}
return s;
}

static void Main(string[] args)
{
DateTime now = DateTime.Now;
Stack s1 = GetStackB4Generics();
double sum1 = 0;
while (s1.Count != 0)
{
sum1 += (int)s1.Pop(); //cast
}
Console.WriteLine("Sum of stack 1 is " + sum1);
Console.WriteLine(DateTime.Now.Subtract(now));
now = DateTime.Now;

Stack s2 = GetStackAfterGenerics();
double sum2 = 0;
while (s2.Count != 0)
{
sum2 += s2.Pop(); //no cast
}
Console.WriteLine("Sum of stack 2 is " + sum2);
Console.WriteLine(DateTime.Now.Subtract(now));

}
}
}

Convert html to xhtml using python

In python on November 3, 2010 at 10:28 pm

Converting html to xhtml is a boring task. Recently, I’ve been working on a small project that requires  xhtml input, precisely an html code where all open tags must have the corresponding close one. That leads me to the need to write a small program which takes as input a local html file or an address (ex:http://google.com) and gives as output the corresponding xhtml file. You can download it at:

http://svn.assembla.com/svn/4a_project/trunk/html2xhtml.tar.gz

Extract the download file. For running the program, type

./html2xhtml.py url(local file or link) output_file

Note that, if url is not a file, it must have prefix like http:// or ftp://

The program is entirely coded in python, so is is portable. In windows, maybe you need to convoke the script manually by typing

python html2xhtml.py url(local file or link) output_file

You can also use the class html2xhtml as a small library in your program.

Of course, some program like the one at http://www.it.uc3m.es/jaf/html2xhtml/ is by far complete and offers a lot more of functionality.

What is Project Euler and why should we participate in?

In Project Euler on August 15, 2010 at 8:54 am

Leonhard Euler

Project Euler is a website that proposes a series of mathematical problems but usually need to be solved by a computer scientist. Some are quite easy and can be done by “brute force”, i.e. scanning all the possibilities without any “sieve”.
So why should we participate in? There are two main reasons for anyone, not necessarily in the domain of computer science or mathematics to attack some:

  • To see mathematical problems in a different way, maybe less beautiful than the pure one, but surely more practical.
  • To understand  how  a program works in a machine, be conscious of each Byte used in memory, etc.

Project Euler has been for me an interesting adventure: it has helped me to get out of FM(Football Manager) world when it seems hopeless and gave me a different point of view towards modern mathematicians.
My next posts will be concentrate on problems on Project Euler, their traps and their solutions.

Follow

Get every new post delivered to your Inbox.