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

  • Home
  • SEARCH
  • 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 8831551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:20:07+00:00 2026-06-14T08:20:07+00:00

I have multiple enumerators that enumerate over flat files. I originally had each enumerator

  • 0

I have multiple enumerators that enumerate over flat files. I originally had each enumerator in a Parallel Invoke and each Action was adding to a BlockingCollection<Entity> and that collections was returning a ConsumingEnumerable();

public interface IFlatFileQuery
{
    IEnumerable<Entity> Run();
}

public class FlatFile1 : IFlatFileQuery
{
    public IEnumerable<Entity> Run()
    {
        // loop over a flat file and yield each result
        yield return Entity;
    }
} 

public class Main
{
    public IEnumerable<Entity> DoLongTask(ICollection<IFlatFileQuery> _flatFileQueries)
    {
            // do some other stuff that needs to be returned first:
            yield return Entity;

            // then enumerate and return the flat file data
        foreach (var entity in GetData(_flatFileQueries))
        {
            yield return entity;
        }
    }

    private IEnumerable<Entity> GetData(_flatFileQueries)
    {
        var buffer = new BlockingCollection<Entity>(100);

        var actions = _flatFileQueries.Select(fundFileQuery => (Action)(() =>
        {
            foreach (var entity in fundFileQuery.Run())
            {
                buffer.TryAdd(entity, Timeout.Infinite);
            }
        })).ToArray();

        Task.Factory.StartNew(() =>
        {
            Parallel.Invoke(actions);

            buffer.CompleteAdding();
        });

        return buffer.GetConsumingEnumerable();
    }
}

However after a bit of testing it turns out that the code change below is about 20-25% faster.

private IEnumerable<Entity> GetData(_flatFileQueries)
{
    return _flatFileQueries.AsParallel().SelectMany(ffq => ffq.Run());
}

The trouble with the code change is that it waits till all flat file queries are enumerated before it returns the whole lot that can then be enumerated and yielded.

Would it be possible to yield in the above bit of code somehow to make it even faster?

I should add that at most the combined results of all the flat file queries might only be 1000 or so Entities.

Edit:
Changing it to the below doesn’t make a difference to the run time. (R# even suggests to go back to the way it was)

private IEnumerable<Entity> GetData(_flatFileQueries)
{
        foreach (var entity in _flatFileQueries.AsParallel().SelectMany(ffq => ffq.Run()))
        {
            yield return entity;
        }
}
  • 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-14T08:20:08+00:00Added an answer on June 14, 2026 at 8:20 am

    The trouble with the code change is that it waits till all flat file queries are enumerated before it returns the whole lot that can then be enumerated and yielded.

    Let’s prove that it’s false by a simple example. First, let’s create a TestQuery class that will yield a single entity after a given time. Second, let’s execute several test queries in parallel and measure how long it took to yield their result.

    public class TestQuery : IFlatFileQuery {
    
        private readonly int _sleepTime;
    
        public IEnumerable<Entity> Run() {
            Thread.Sleep(_sleepTime);
            return new[] { new Entity() };
        }
    
        public TestQuery(int sleepTime) {
            _sleepTime = sleepTime;
        }
    
    }
    
    internal static class Program {
    
        private static void Main() {
            Stopwatch stopwatch = Stopwatch.StartNew();
            var queries = new IFlatFileQuery[] {
                new TestQuery(2000),
                new TestQuery(3000),
                new TestQuery(1000)
            };
            foreach (var entity in queries.AsParallel().SelectMany(ffq => ffq.Run()))
                Console.WriteLine("Yielded after {0:N0} seconds", stopwatch.Elapsed.TotalSeconds);
            Console.ReadKey();
        }
    
    }
    

    This code prints:

    Yielded after 1 seconds
    Yielded after 2 seconds
    Yielded after 3 seconds

    You can see with this output that AsParallel() will yield each result as soon as its available, so everything works fine. Note that you might get different timings depending on the degree of parallelism (such as “2s, 5s, 6s” with a degree of parallelism of 1, effectively making the whole operation not parallel at all). This output comes from an 4-cores machine.

    Your long processing will probably scale with the number of cores, if there is no common bottleneck between the threads (such as a shared locked resource). You might want to profile your algorithm to see if there are slow parts that can be improved using tools such as dotTrace.

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

Sidebar

Related Questions

I have a function that needs to enumerate an iterator multiple times, but according
i have multiple files each containing 8/9 columns. for a single file : I
I have multiple zip files that have the same structure -- they contain XML
I have multiple table rows that has a set of radio buttons on each
I have multiple runnable jar files. Each of these jar files references the same
I have multiple .xls files that I want to convert to csv files and
I have multiple tool bars that are all next too each other. I want
I have multiple css files in a directory (style0.css, style1.css etc..) How can I
I have multiple autocomplete inputs on my page. Behind the scenes, each autocomplete list
I have multiple divs, each with a unique id and image. I would like

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.