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 5941501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:09+00:00 2026-05-22T16:07:09+00:00

EDIT Additional options and a slightly extended question below. Consider this contrived and abstract

  • 0

EDIT Additional options and a slightly extended question below.

Consider this contrived and abstract example of a class body. It demonstrates four different ways of performing a “for” iteration.

private abstract class SomeClass
{
    public void someAction();
}

void Examples()
{
    List<SomeClass> someList = new List<SomeClass>();

    //A. for
    for (int i = 0; i < someList.Count(); i++)
    {
        someList[i].someAction();
    }

    //B. foreach
    foreach (SomeClass o in someList)
    {
        o.someAction();
    }

    //C. foreach extension
    someList.ForEach(o => o.someAction());

    //D. plinq
    someList.AsParallel().ForAll(o => o.someAction());

EDIT: Addition of some options from answers and research.

    //E. ParallelEnumerable
    ParallelEnumerable.Range(0, someList.Count - 1)
        .ForAll(i => someList[i].someAction());

    //F. ForEach Parallel Extension
    Parallel.ForEach(someList, o => o.someAction());

    //G. For Parallel Extension
    Parallel.For(0, someList.Count - 1, i => someList[i].someAction())
}

My question comes in two parts. Have I missed some significant option? Which option is the best choice, considering readability but primarily performance?

Please indicate if the complexity of the SomeClass implementation, or the Count of someList would effect this choice.

EDIT: With such a dizzying array of options, I wouldn’t like my code to be spoilt by choice. To add a thrid part to my question, If my list could be any length should I default to a parallel option?

As a straw man. I suspect that over all implementations of SomeClass and all lengths of someList option //E. ParallelEnumerable would offer the best average performance, given the prevalanece of multi processor architechtures. I haven’t done any testing to prove this.

Note: The parallel extensions will require the use of the System.Threading.Tasks namespace.

  • 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-22T16:07:10+00:00Added an answer on May 22, 2026 at 4:07 pm

    Option A only really makes sense for sequences that implement indexing and will only be performant for those that have O(1) lookup time. Generally, I would use the foreach and variants unless you have special logic.

    Also note, that “special logic” like for (int i = 1; i < list.Count; i++) can be implemented with Linq extension methods: foreach(var item in sequence.Skip(1)).

    So, generally prefer B over A.

    As to C: This can be confusing for other developers if they aren’t used to the functional style.

    As to D: This will depend on a lot of factors. I guess for simple calculations, you don’t want to do this – you will only really benefit from parallelization if the loop body takes a while to compute.

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

Sidebar

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: I have solved this by myself. See my answer below I have set
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
This is a purely pedantic question, to sate my own curiosity. I tend to
Apologies if this question has been answered before. Though the answers I've seen look
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business

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.