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.

2 comments:

Anonymous said...

Hey Nat,

Your Blog rocks!! Just wanted to share something with ya… one blogger to another…
There is this amazing site that I came across where u can make money by sharing information…check it out here’s the link http://www.myndnet.com/login.jsp?referral=alpa83&channel=al278

The coolest part is…every time ur information gets sold u get paid for it!!
I signed it for it.. very cool stuff… u can also mail me at barot.alpa@gmail.com

Cheers!
Alpa

IncrediMail Support said...

In addition, IncrediMail also provides resolution to the users by protecting them from fraud attempts and spams. Since it is a user-friendly web-based client, therefore, it offers optimum support of self-explaining guides to its users. For any technical problems related to IncrediMail, dial IncrediMail Support Phone NUmber and enjoy the ease of resolution to your problems. If you are dealing with IncrediMail glitch, just get in touch at our toll-free helpline number and enjoy 24*7 services on a single call. Problems like account loading, fetching accounts' data, settings or configuration, email inbox, and outbox issues will be sorted out within seconds. We have a team of experts and professionals who assist users to use their accounts in the best possible ways.