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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:54:00+00:00 2026-06-04T19:54:00+00:00

I have a simple while loop IEnumerable<Foo> collection; while (!bc.IsCompleted) { collection = bc.Take();

  • 0

I have a simple while loop

IEnumerable<Foo> collection;
while (!bc.IsCompleted)
{
   collection = bc.Take();
}

bc is a BlockingCollection<IEnumerable<Foo>>. The bc contains 9 IEnumerable collections and a total of 2.6million Foo objects. The loop takes about 640ms to run on my machine. As soon as I add a foreach loop after Take() within the while loop the time it takes to run explodes to 2400ms.

foreach(Foo foo in collection)
{
}

Iterating over 2.6 million elements within a List or Foo[] or IEnumerable that I setup separately, took about 54ms.

The same happens if instead of the foreach loop I simply add a collection conversion such as

List<Foo> fooList = collection.ToList();

or

Foo[] fooArray = collection.ToArray();

It suddenly also takes north of 2000ms to execute.

How can this be? I am completely running out of explanations or possible reasons. Anyone who could point me to what I am missing here? The slow down cannot be caused by locking/blocking because I do not alter the way the BlockingCollection is accessed between my comparisons.

Thanks for any input.

  • 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-06-04T19:54:03+00:00Added an answer on June 4, 2026 at 7:54 pm

    As with others LINQ methiods out there (and im guessing you are using LINQ’s take), this method works with a deffered execution:

    This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic

    This means that if you dont add the ToList() call of the foreach loop, the sole call to Take doesnt really produce any results, and the actuall results are only brought when you are using the iterator (of foreach/tolist), hence the performance difference.

    Your comparison to just iterating over list might not provide accurate results; Its not the iteration over the List(foo) that takes time, its probably the selection of the elements from the blocking collection you are using that slows everything down.

    MSDN claims that using regular foreach on the BlockingCollection (which is probably what happens when u r using the LINQ supplied Take, which works on IEunmerable in this case) uses a snashop of the underlying collection, and this can surely slow done the processing on huge collections.

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

Sidebar

Related Questions

I have a script that uses a simple while loop to display a progress
I have setup a simple while loop which returns all images in a table
I have written a simple script that calls a function in a while loop.
I have a simple while loop that checks the database for inserts. If its
I have this very simple Wordpress while loop: $loop = new WP_Query( args )
I have a simple python script that just runs an infinite while loop and
Have a simple while loop and trying to make it to a for loop
I have this simple while loop which retrieves data from a mysql query and
I have a simple while loop i'm trying to implement but for the life
I have a simple function that shows loading spinner while fetching data (usually takes

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.