Sunday, March 18, 2007

Taking advantage of duo core processor

I have heard for a while that running a multi-thread program on multi core processor is a lot faster than running it on single core one. So, I decided to conduct a mini-experiment to test whether this is true.

C# Console Application, .NET Framework 2.0


Program.cs


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PerformanceTester
{
class Program
{
private const int NUM = 5000000;

static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
DateTime start;
TimeSpan span;

start = DateTime.Now;
SingleThreaded();
span = DateTime.Now.Subtract(start);
Console.WriteLine(
String.Format("\tSingle Threaded : {0} ms",
span.TotalMilliseconds));

start = DateTime.Now;
DoubleThreaded();
span = DateTime.Now.Subtract(start);
Console.WriteLine(
String.Format("\tDouble Threaded : {0} ms",
span.TotalMilliseconds));

}
Console.Read();

}

private static void SingleThreaded()
{
PrimeGenerator generator = new PrimeGenerator();
List<int> results = new List<int>();
generator.AddPrimes(results, 1, NUM);
}

private static void DoubleThreaded()
{
List<int> results = new List<int>();

Thread t1 = new Thread(delegate()
{
PrimeGenerator generator =
new PrimeGenerator();
generator.AddPrimes(results,1,NUM/2);
});

Thread t2 = new Thread(delegate()
{
PrimeGenerator generator =
new PrimeGenerator();
generator.AddPrimes(results,NUM/2+1,NUM);
});

t1.Start(); t2.Start();
t1.Join(); t2.Join();
}
}
}

PrimeGenerator.cs




using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PerformanceTester
{
class PrimeGenerator
{
private bool IsPrime(int n)
{
if (n <= 1)
return false;
else if (n <= 3)
return true;
else if (n % 2 == 0)
return false;
else
{
int bound = Convert.ToInt32(Math.Ceiling(Math.Sqrt(n)));
for (int i = 3; i <= bound; i++)
{
if (n % i == 0)
return false;
}
return true;
}
}

public void AddPrimes(List<int> list,int lowerBound,int upperBound)
{
for (int number = lowerBound; number < upperBound; number++)
{
if(IsPrime(number))
lock (list)
{
list.Add(number);
}
}
}
}
}
This program will search for all primes between 1 and 5000000 inclusive. It works in two mode, single threaded and double threaded. The search range is divided into two parts in double threaded mode, 1 - 2500000 and 2500001 - 5000000 (we can obviously see that the work load of the second range is bigger than the first one but since this is informal experiment so I did not care about this point :P). The program also record running times for each mode and print them on the screen.

Here are the results:

Running on my IBM R52 - Intel(R) Pentium(R) M 1.60 GHz
(Single core)

Single Threaded : 11687.5 ms
Double Threaded : 12875 ms
Single Threaded : 12812.5 ms
Double Threaded : 12953.125 ms
Single Threaded : 12000 ms
Double Threaded : 11703.125 ms

Running on my mom's IBM R60 - Intel(R) Core(TM)2 CPU 15500 @ 1.66GHz
(Double core)

Single Threaded : 9171.875 ms
Double Threaded : 5625 ms
Single Threaded : 9109.375 ms
Double Threaded : 5625 ms
Single Threaded : 9109.375 ms
Double Threaded : 5609.375 ms

For single core, the running time is practically the same for each mode. While for double core, double threaded mode gives noticeably better running time.

So, to take advantage of new duo core processors nowadays, considers building your program to support multi threaded mode.

Monday, March 5, 2007

Summer' resolution

Tired from taking many exams last week (being a Thai student in a Thai university is not that easy, you know?), I decided to spend my very first time of my summer sitting in front of my computer, playing PS2 games with my brother and help my sister to prepare for upcoming Triamudom suksa school entrance exam.

At summers or any long vacations, I always make resolutions. ... I will do these, I will do those, I will learn C#, I will prepare for MCPD, ... blah blah. But up until this day, only 30% of all my resolutions had been done. So I will not make too many resolutions this summer ;)

My resolution for this summer is that I will finish reading my two new books within this summer. They are ...




Practical Object-Oriented Design With UML by Mark Priestly - After reading first few chapters of this book, I am really impressed at how the writer described the software engineering process. Compared to other books in the same category, this book explains SE in more friendly way. I really like this kind of books.

This book focuses at the use of UML with software projects. It first briefly explains the popular methodologies used in SE world which are Unified Process and eXtreme programming. After that, there is a real-world case study (restaurant booking system) and a UML tutorials.

I found this book at my university's book shop, the price is incredibly low for an English text book in Thailand (around 500 THB or 12 USD).



Code Complete 2nd Edition by Steve McConnell - Have heard of this book many many times, I decided to buy a copy. As a MSDN Connection Gold member, I could buy this book from MS Press partner with 35% off or around 1300 THB (Actually, I could asked someone I know who has a platinum card to buy this for me at 40% off!).

The book focuses on the best practices for everything in building a software from coding level to software project management. Some also says that this is a book every serious programmers should have.

Hope I can finish these before my third year at the university begins.

Sunday, March 4, 2007

Access modifier : "private" for distinct instances of the same class

Many of us who are reading this blog might have learned the use of access modifiers in Java. I found that there is a small confusion about the use of "private" access modifier.

private
member
Accessible only in its class(which defins it).
protected
member
Accessible only within its package and its subclasses
public
class

interface

member

Accessible anywhere

Accessible anywhere

Accessible anywhere its class is.

Let us create a new class named "Student".

public class Student {
private int score;

public Student() {
score = 89;
}

public void modifyScore(Student that,int newValue){
that.score = newValue;
}

public String toString() {
return "Score = " + score;
}

}


Imagine a situation where instance "A" of class "Student" trying to modify the "score" member of instance "B" of the same class, The misunderstanding is this cannot be done because of the score's access modifier is private. In fact, it can be done correctly. As the rule states that private members are accessible only in its class. So instances could access the private members of other instances too.

Windows Vista : Upgrade versus Clean Install


I have just clean installed the final version of Windows vista few hours ago. The installation process took only 30 minutes! That was fast, compared to previous versions of Windows.

However, when I installed Windows Vista (RC1) for the first time. It took around 2-2.5 hours for the installation process to finish. I was using the "Upgrade" option at that time to upgrade from my Windows XP SP2.

So for those who are planning to upgrade to Windows Vista, my suggestion is to do the clean install instead of upgrade from previous version. Addition to the shorter setup time, I think clean installation gives the system more stabilities too.