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.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: