Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:52:08+00:00 2026-05-16T23:52:08+00:00

Looking for a little advice on leveraging AsParallel() or Parallel.ForEach() to speed this up.

  • 0

Looking for a little advice on leveraging AsParallel() or Parallel.ForEach() to speed this up.

See the method I’ve got (simplified/bastardized for this example) below.

It takes a list like “US, FR, APAC”, where “APAC” is an alias for maybe 50 other “US, FR, JP, IT, GB” etc. countires. The method should take “US, FR, APAC”, and convert it to a list of “US”, “FR”, plus all the countries that are in “APAC”.

private IEnumerable<string> Countries (string[] countriesAndAliases)
{
    var countries = new List<string>();

    foreach (var countryOrAlias in countriesAndAliases)
    {
        if (IsCountryNotAlias(countryOrAlias))
        {
            countries.Add(countryOrAlias);
        }
        else 
        {
            foreach (var aliasCountry in AliasCountryLists[countryOrAlias]) 
            {
                countries.Add(aliasCountry);
            }
        }
    }

    return countries.Distinct();
}

Is making this parallelized as simple as changing it to what’s below? Is there more nuance to using AsParallel() than this? Should I be using Parallel.ForEach() instead of foreach? What rules of thumb should I use when parallelizing foreach loops?

private IEnumerable<string> Countries (string[] countriesAndAliases)
{
    var countries = new List<string>();

    foreach (var countryOrAlias in countriesAndAliases.AsParallel())
    {
        if (IsCountryNotAlias(countryOrAlias))
        {
            countries.Add(countryOrAlias);
        }
        else 
        {
            foreach (var aliasCountry in AliasCountryLists[countryOrAlias].AsParallel()) 
            {
                countries.Add(aliasCountry);
            }
        }
    }

    return countries.Distinct();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:52:08+00:00Added an answer on May 16, 2026 at 11:52 pm

    Several points.

    writing just countriesAndAliases.AsParallel() is useless. AsParallel() makes part of Linq query that comes after it execute in parallel. Part is empty, so no use at all.

    generally you should repace foreach with Parallel.ForEach(). But beware of not thread safe code! You have it. You can’t just wrap it into foreach because List<T>.Add is not thread safe itself.

    so you should do like this (sorry, i didn’t test, but it compiles):

            return countriesAndAliases
                .AsParallel()
                .SelectMany(s => 
                    IsCountryNotAlias(s)
                        ? Enumerable.Repeat(s,1)
                        : AliasCountryLists[s]
                    ).Distinct();
    

    Edit:

    You must be sure about two more things:

    1. IsCountryNotAlias must be thread safe. It would be even better if it is pure function.
    2. No one will modify AliasCountryLists in a meanwhile, because dictionaries are not thread safe. Or use ConcurrentDictionary to be sure.

    Useful links that will help you:

    Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4

    Parallel Programming in .NET 4 Coding Guidelines

    When Should I Use Parallel.ForEach? When Should I Use PLINQ?

    PS: As you see new parallel features are not as obvious as they look (and feel).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard conflicting stories on this topic and am looking for a little
Was looking for the answer on the net, but couldn't find anything. This little
I'm looking for a little database design advice... I have a spreadsheet with a
I'm new with Qt and am looking for advice on how to structure this.
I'm looking for a little sage advice from some helpful HTML/CSS masters. I am
I am looking for a little advice on database design. I am constructing a
I am looking for a little advice on structuring the following. I would like
I know this is a little subjective, but I'm looking into the following situation:
I'm looking for a little help structuring a web form to include PHP validation
I'm just looking for a little guidance as to how i might implement a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.