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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:01:04+00:00 2026-06-13T14:01:04+00:00

The following code runs in roughly 2.5 seconds: static void Main(string[] args) { var

  • 0

The following code runs in roughly 2.5 seconds:

static void Main(string[] args)
{
    var service = new Service();
    Parallel.For(0, 100, i => {
        dynamic user = new ExpandoObject();
        user.data = new ExpandoObject();
        user.data.id = i;
        user.data.name = "User Name";
        var parsed = service.Parse(user);
    });
}

public class Service
{
    public User Parse(dynamic dynamicUser)
    {
        if (dynamicUser.data != null)
        {
            return new User
            {
                Id = dynamicUser.data.id,
                Name = dynamicUser.data.name
            };
        }
        return null;
    }
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

However, if I change the Parallel.For() loop to a simple For loop, it runs in about 200 miliseconds:

for (var i = 0; i < 100; i++)

So my question is, why is this much slower when run in parallel?

My theory is that there is some overhead in parsing the dynamic object that is done once per thread. In the simple loop, the DLR does its thing the first time and then doesn’t need to for each subsequent call.

But in parallel, the overhead of the DLR happens in each call.

Is this a correct assumption, or am I way off base?

  • 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-13T14:01:05+00:00Added an answer on June 13, 2026 at 2:01 pm

    I suspect you’re being mislead by your diagnostics. In particular, if running a loop 100 times takes 2.5 seconds, that’s really, really slow. Is this under the debugger, by any chance?

    Here are the results on my box for code compiled with /o+ and then run in the console. Note that I’m running 1,000,000 loop iterations in each test.

    Void ExecuteParallel(): 00:00:00.7311773
    Void ExecuteSerial(): 00:00:02.0514120
    Void ExecuteParallel(): 00:00:00.6897816
    Void ExecuteSerial(): 00:00:02.0389325
    Void ExecuteParallel(): 00:00:00.6754025
    Void ExecuteSerial(): 00:00:02.0653801
    Void ExecuteParallel(): 00:00:00.7136330
    Void ExecuteSerial(): 00:00:02.0477593
    Void ExecuteParallel(): 00:00:00.6742260
    Void ExecuteSerial(): 00:00:02.0476146
    

    It’s not as much faster in parallel as you might expect from a quad-core i7, but I suspect that’s due to the context switches etc mentioned by Servy – and also possibly contention on the execution cache in the DLR. Still, it’s faster than running in series.

    Try the code yourself, and see what you get on your box – but not under a debugger.

    Code:

    using System;
    using System.Diagnostics;
    using System.Dynamic;
    using System.Threading.Tasks;
    
    class Test
    {
        const int Iterations = 1000000;
    
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                RunTest(ExecuteParallel);
                RunTest(ExecuteSerial);
            }
    
        }
    
        static void RunTest(Action action)
        {
            var sw = Stopwatch.StartNew();
            action();
            sw.Stop();
            Console.WriteLine("{0}: {1}", action.Method, sw.Elapsed);
        }
    
        static void ExecuteParallel()
        {
            var service = new Service();
            Parallel.For(0, Iterations, i => {
                dynamic user = new ExpandoObject();
                user.data = new ExpandoObject();
                user.data.id = i;
                user.data.name = "User Name";
                var parsed = service.Parse(user);
            });
        }
    
        static void ExecuteSerial()
        {
            var service = new Service();
            for (int i = 0; i < Iterations; i++)
            {
                dynamic user = new ExpandoObject();
                user.data = new ExpandoObject();
                user.data.id = i;
                user.data.name = "User Name";
                var parsed = service.Parse(user);
            }
        }
    }
    
    public class Service
    {
        public User Parse(dynamic dynamicUser)
        {
            if (dynamicUser.data != null)
            {
                return new User
                {
                    Id = dynamicUser.data.id,
                    Name = dynamicUser.data.name
                };
            }
            return null;
        }
    }
    
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why the following code runs so.... slow....... ? <html><body><script type=text/javascript> var i = 0;
I have the following code: public static long CreateVendorsEmailJob(List<Recipient> recipients, string subject, string body)
The following code runs fine on AppHarbor . public virtual void button1Clicked (object sender,
I'm trying to create a new CodeType, the following code runs just fine in
The following code runs without any errors but doesn't actually delete anything: $update =
The following code runs fine under Magento 1.6 but raises a Mage_Core_Exception (message: 'Cannot
i've got the following code which runs a bat file. the bat file then
The following piece of code runs fine when parallelized to 4-5 threads, but starts
I have the following jQuery code which runs when I'm clicking an option in
I have the following SQL code that runs against a Change Request database. Each

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.