Posts Tagged ‘Performing’

If you have a multiple processors (or multi-core processor), you can use the Processor Affinity to direct a specific process to use a specified core. By this way, if your process always use the same core, the process will run more efficiently because of the cache re-use.

You can set the Processor Affinity in the task manager.

Open the task manager, choose your application and go to process.

Go to the process

Go to process

Right click on the process and choose “Set Affinity”

Set Affinity

Set Affinity

Then choose which processor or processors your process will run on.

Processor Affinity

Processor Affinity

Setting the Processor Affinity programatically in C#

.Net gives you the ability to determine which core or cores your application will run on. You have to use the System.Diagnostics namespace which contains the class Process. This class contains the ProcessorAffinity property that can be used to either get or set the processor affinity.

Notice that the ProcessorAffinity property is a bit mask where each bit represents a single processor. The following table shows a selection of ProcessorAffinity values for an eight-processor system.


Binary value

Eligible processors

0x0001 00000000 00000001 1
0x0003 00000000 00000011 1 and 2
0x0007 00000000 00000111 1, 2 and 3
0x0009 00000000 00001001 1 and 4
0x007F 00000000 01111111 1, 2, 3, 4, 5, 6 and 7

The following example shows the processor affinity of the current process and then change the processor affinity to processor 1 and 2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ProcessorAffinityExample
    class Program
        static void Main(string[] args)
            using (Process myProcess = Process.GetCurrentProcess())
                Console.WriteLine("ProcessorAffinity: {0}", myProcess.ProcessorAffinity);

                Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)3;

                Console.WriteLine("ProcessorAffinity: {0}", myProcess.ProcessorAffinity);


Read Full Post »