Assign Processor Affinity to Improve Performance

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.00 out of 5)
Loading ... Loading ...

August 27th, 2010 written by Brian in Windows Insights

Do you own a computer with multi-core processors? Have you heard about processor affinity? If not, you’ll learn here what it is and how it might help IT pros get more control over the performance of their applications.

A little bit of background—many computers these days have multiple core processors. Unfortunately, there aren’t that many applications out there that take advantage of these processors by recognizing multi-threading. However, even single-threaded applications can take advantage of multi-core processors.

What Exactly is Processor Affinity?

Processor affinity is the ability to direct a process to use specific cores. By default, all processes use all of the cores available on the system. If it’s set by default, using all of the cores must be the best choice, right? So why would we want to restrict a process to only certain cores? Actually, there might be a reason for you.

Processor Caching

If a certain task or process runs frequently, there would be remains of the previous process run left in the process cache.

Controlling processor affinity can improve performance by reducing the number of cache flushes when process threads move to another processor. If the process is directed to use the same core everytime, performance can be improved due to the ability to re-use the cache.

What Are the Benefits of Processor Affinity?

For most programs, processor affinity probably won’t make any difference, but it helps when users are running, for example, anti-virus scans, a game, a browser with heavy Flash apps or something else that uses up a lot of the CPU. If two of these intensive programs are running at the same time—and using all of the possible processor cores—they would probably negatively impact each other’s performance.

But by isolating critical processes to one core and leaving the rest of them to the other core, it allows the critical processes to receive the full power of the processor core, and it does this without affecting the performance of the remainder of the processes.

Note to readers: Check out the post titled “Incredibly Sluggish PC? How to Easily Detect and Combat High Processor Usage” to learn about another interesting way of automatically handling process priorities!

How Do We Enable Processor Affinity?

To enable processor affinity, start by going to Windows Task Manager by using the Ctrl + Shift + Esc shortcut. Navigate to the Processes tab.

Right-click on the process you want to set an affinity for, such as a backup program or a media player.

You will then be presented with a screen similar to the following. It might be slightly different depending on the operating system you’re using and the number of processor cores you have. Select which processors you want to allow the process to utilize. Note that CPU 0 corresponds to the first core, CPU 1 corresponds to the second, CPU 2 the third and so on.

Press OK, and then you’re done. Repeat as desired for other processes.

Unfortunately, you may notice that this needs to be set everytime the process runs. To automatically have it set, you need to edit the program’s shortcut.

Go to a shortcut icon for the program you want to set an affinity for, and right-click on it. Select Properties. Then head to the Shortcut tab.

Under the Target field, copy down what’s already there. Replace it with the following:

C:\WINDOWS\system32\cmd.exe /c  start  “(Name of Application).exe” /affinity (Affinity Number) “(Path to the exe file)”

Make sure you keep all of the quotes the way they are. Replace Name of Application with the name of the exe file such as firefox.exe. Replace Path to exe file with the path such as C:\Program Files\Mozilla Firefox\firefox.exe.

For the /affinity tag, the number depends on what affinity you want to set. To find the affinity number, do the following:

  1. List your CPUs horizontally backwards. For quad core, it would look like: CPU 3, CPU 2, CPU 1, CPU 0.
  2. Convert the ones you want the process to use into 1 and the rest into 0. If I wanted firefox.exe to use CPU 3 and CPU 0, I would have the string 1001.
  3. This is binary. Convert your binary string into decimal. There are many tools online to do this, such as this one.
  4. For this example with 1001, it would be /affinity 9.

If I wanted the process firefox.exe to use CPU 3 and CPU 0, the target field would read:

C:\WINDOWS\system32\cmd.exe /c  start  “firefox.exe” /affinity 9 “C:\Program Files\Mozilla Firefox\firefox.exe”

After you’re done setting up the Target field, press OK and Apply, and try out your new shortcut. The program should automatically open using the proper CPU. You can check by going back to Task Manager.

Setting processor affinity can help improve performance with certain single-threaded programs, especially those that tend to consume a lot of CPU. However, be aware that for many programs, there will not be a noticeable performance improvement.

How is processor affinity working for you? Do you have a better method of doing this? Let us know in the comments.

23 Responses to “Assign Processor Affinity to Improve Performance”

Trackbacks/Pingbacks

Comments

  1. Congrats Brian, For such an awesome post !

  2. Hi Brian,
    Thank You Very Much For The Great Article!
    I’m Very Happy To See Young Creative Friends Sharing
    Experiences,That’s Well Drive More Ideas And Benefit More
    People…
    Also I Would Thank TuneUp Site Team To Give Us This
    Wonderful Platform To Sharing Our Ideas And Learn More
    About Technology’s…And Know More Great Friends…
    Thanks Again Dear Brian,Keep The Good Work!!!
    Respectfully,
    Elina.

  3. P.S.,Nice Website You Have!,I Like it!

  4. When assigning a process/job to an affinity; would it make any difference which cores to use first? 0/1/2/3, to use 2&3 or 0&1 or just use any single core? When I am running 2 video conversion job; I assign one job to cores 2&3 and the other to cores 0&1, and everything seems to slow to unacceptable level. My cpu is a phenom II x4 B60 3.3ghz. Can you tell me how should I arrange the affinities to get better performance? Many thanks.

  5. Hello, I have tried doing this but I keep getting an error that the program cannot be found. For some odd reason, if I try to run Mozilla with the default processor affinity, it often hangs an unacceptable number of times and I found that reducing it to one core seemed to fix this. I read your article and thought it would be great to set it permanently but I can’t seem to get it to execute properly. I’ve triple-checked to make sure that I was writing it in properly and I was, but still the same thing. So I copied the line directly from the article and switched the affinity number to match what I was trying to do and still the same thing. Even tried using different cores but that seemed to make no difference. I back-tracked the program location to make sure that it was indeed in the location I was specifying and it is, so what am I doing wrong? This is the line I have written into the target field:

    C:\WINDOWS\system32\cmd.exe /c start “firefox.exe” /affinity 2 “C:\Program Files\Mozilla Firefox\firefox.exe”

    • Hello. I don’t believe Windows XP supports the /affinity switch. I tested these steps with Vista and above.

      I just tested these commands with XP (and confirmed that the Affinity option was available in Task Manager) and /affinity doesn’t seem to be supported.

      I’ll look into alternate ways to permanently set a program to open with a certain affinity for Windows XP.

  6. Actually, as interesting as it would be to know how to set affinity permanently in XP, it seems like that doesn’t fix Mozilla at all. I have no idea why it does that. It used to be the best browser out there but now it’s locking up and hanging upward of 18 times in 2 minutes. It’s virtually unusable. If you have any suggestions to fix that, I’d be extremely grateful. I’ve been using Opera, but a lot of things don’t seem to work with it, forcing me to use Mozilla to check my e-mail among other things. I don’t want to use IE or Chrome, so that severely limits my options. Wish Mozilla would just fix Firefox because as far as I know, this is a very common problem but I’ve yet to see a working fix for this.

    • What other software or extensions do you have installed? In the past, I always had a problem with Firefox locking up (with a process that won’t end) when I had ZoneAlarm Firewall installed and running.

  7. Hey Brian,
    I like the article great job! The step where you instruct readers to copy down and replace the file path is a little confusing. My suggestion is to state in that step the fact that the “Target” field is equivalent to the “Path to the exe file.” It may sound extraneous but I know a few people who have difficulty in understanding technical terms because they are unfamiliar with the setting.
    Respectfully,
    Aaron.

  8. Hi Brian,

    Did you ever find away to make this work on shortcuts for Win XP?

  9. Very helpful. However, the number for the /affinity tag is actually hexadecimal rather than decimal.

  10. hi. thanks ! pretty useful.

  11. Nice one brian

    Thanks

  12. Thank you for posting this. My dual-core AMD has problems with runaway Zynga games (high CPU consumption, a gig of RAM) and a program called TuneUp that cleans up iTunes tags. By silo’ing these processes into a single core, I can still do other things and maintain of Windows itself from the safety of the other core. Thanks!!

  13. brian whats the best core to use for an online game in your opinion?

  14. Quote “Setting processor affinity can help improve performance with certain single-threaded programs, especially those that tend to consume a lot of CPU. However, be aware that for many programs, there will not be a noticeable performance improvement.”

    so whats it all good for. if it aint good for your performance ?

    • in regards to soFake. It is useful for exactly what was stated, no one said it is ‘not’ good for performance. It simply tells your processor to isolate a programs threads to specific cores rather than all available, which in turn leaves cores free for other tasks = less overhead = better performance. If a program is optimized for dual core and it defaults using all 6 of your cores, you are generating excess overhead on your cpu cores therefor it would be in your best interest to set affinity to 2 cores leaving others free. Not all programs will benefit or have a clearly obvious effect for multiple reasons. Depending on the cpu intensity of the program and the optimization for multiple cores, if there is a difference it may be very slight, if at all especially for minor processes which do not take much cpu to run at all. Alot of games given how they are optimized ‘will’ benefit from running on 1-2 cores vs all of them, especially if you generally multitask while playing said games.


Leave a commentLogin | Register

*

SDFgvxcvlerRtv
vm-blog02