Вопрос

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?

Это было полезно?

Решение

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; }
}

Другие советы

Here the tasks that you're doing are so simple, take so little time, and there are few enough of them, that the overhead of creating threads, breaking up the tasks, scheduling them, dealing with context switches, memory barriers, and all of that, is significant in comparison to the amount of productive work that you're doing. If you were doing work that took longer then the overhead of parallelizing it would be much less in comparison.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top