Why is the performance so different from seemingly the same code?

I have a WCF service called TapApiService

which contains the following methods:

public class TapApiService : Contracts.ServiceContracts.ITapApiService
{
    private static readonly IConfigurationManagerWrapper ConfigWrapper = new ConfigurationManagerWrapper();
    private static UriManager uriManager = new UriManager(ConfigWrapper.AppSettings["tapScheme"], ConfigWrapper.AppSettings["tapHost"], ConfigWrapper);

    public JobLongForm GetJob(string jobId)
    {
        var request = uriManager.GetUriFromKey("jobResource", jobId).ToString().GetTapRequest("GET", ConfigWrapper);

        return JsonConvert.DeserializeObject<JobLongForm>(request.GetResponseString());
    }

    public IEnumerable<string> GetAllJobIds()
    {
        return GetAllJobs().Select(job => job.Id);
    }

    public IEnumerable<JobShortForm> GetAllJobs()
    {
        var request = uriManager.GetUriFromKey("allJobsResource").ToString().GetTapRequest("GET", ConfigWrapper);

        return JsonConvert.DeserializeObject<JobShortForm[]>(request.GetResponseString());
    }

    public IEnumerable<JobLongForm> GetFullJobs(IEnumerable<string> jobIds)
    {
        return jobIds.Select(GetJob);
    }
}

      

I have a console application that I am using for testing. It has the following code: var proxy = new TapApiService ();

var stopwatch = new Stopwatch();

stopwatch.Start();
var ids = proxy.GetAllJobIds();
proxy.GetFullJobs(ids);
stopwatch.Stop();
Console.WriteLine(stopwatch.GetElapsedTimeString());

stopwatch.Reset();

stopwatch.Start();
proxy.GetAllJobIds().AsParallel().Select(id => proxy.GetJob(id));
stopwatch.Stop();
Console.WriteLine(stopwatch.GetElapsedTimeString());

      

Results: 00:00:00.713

and 00:00:00.104

Implementation is AsParallel()

clearly much faster. However, if I move AsParallel()

inside the method GetFullJobs(IEnumerable<string> jobIds)

like this:

public IEnumerable<JobLongForm> GetFullJobs(IEnumerable<string> jobIds)
{
    return jobIds.AsParallel().Select(GetJob);
}

      

I get 00:00:00.813

and 00:00:00.158

. The second implementation is even faster. I thought their job would now be the same. What am I missing here?

+3
performance c # task-parallel-library wcf


source to share


No one has answered this question yet

Check out similar questions:

1406
Why does changing 0.1f to 0 slow down performance by 10x?
743
Why is Python code faster in a function?
689
Why is this C ++ code faster than my handwritten assembly for testing the Collatz hypothesis?
499
Why does C ++ compilation take so long?
452
Does anyone have benchmarks (code and results) comparing the performance of Android apps written in Xamarin C # and Java?
439
Is there a performance difference between i ++ and ++ i in C?
61
WCF Service Client: content type text / html; charset = utf-8 of the response message does not match the binding content type
2
What is the best pattern for using LINQ to C #
1
Huge performance difference when the same task is done in self-serving WCF and MVC
0
WCF Guid DataMember is not serializing properly



All Articles
Loading...
X
Show
Funny
Dev
Pics