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


source to share





All Articles