Thursday, 16 April 2009

Code to determine number of CPUs

To get the most out of multicore asynchronous the number of available cpus is need so as to determine the right amount of threads to use. Here are some examples of how to determine this

From Sql the following stored proceedure dilivers a lot of information including the number of cpus
EXEC xp_msver

From C# the following can be used

using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using System.Management;using System.Diagnostics;
namespace NumberOfProcessors
class Program
static void Main(string[] args)
ManagementObjectSearcher mgmtObjects = new ManagementObjectSearcher("Select * from Win32_ComputerSystem");

foreach (var item in mgmtObjects.Get())
Console.WriteLine("Number Of Processors - " + item["NumberOfProcessors"]);
Console.WriteLine("Number Of Logical Processors - " + item["NumberOfLogicalProcessors"]);
catch (ManagementException)
{ }
List processes = Process.GetProcesses().ToList();
Console.WriteLine("All running process");
processes.ForEach(x => Console.WriteLine(x.ProcessName));