My issue, which it seems I couldn't find an answer is that: why if I change the order of calling a method which contains .AsParallel()
and one that doesn't contain, or has different options the result is different.
I've put the source code on GitHub so I don't fill all the screen -> https://github.com/moisoiu/asParallel
And some explication regarding how I've tested it so far.
I'm using other classes to generated some random data and from that point on I'm passing the generated and randomized data to ParallelThreading
methods and measure the time via StopWatch
class and store the results in a dictionary to show at end the response time.
At first I've assumed that the issue with the performance time, it might be because I work on the same reference, so I've created 4 new objects something which is an array so it will have a new reference, but the results we're the same.
At a point I even assumed that maybe (just maybe) the variable from watch might still store the value from previous one, so I've made a new variable for each one
So any ideas?
Just pick a method from Program.cs, and swap it with some other method and there will be almost the same response time but the method that lasted the most will be the one on TOP and the much less one will be on bottom.
Edit: To respect recommendation from comments below. I've attached a part of the source code, In case is needed more, I will update and remove the link from Git above.
Program.cs
class Program
{
static void Main(string[] args)
{
var parallelThreading = new ParallelThreading();
long elapsedMs = 0;
Dictionary<string, long> results = new Dictionary<string, long>();
var dataResults = parallelThreading.InitializeDataForParallelData(6500);
var something = new PersonModel[6500];
var something1 = new PersonModel[6500];
var something2 = new PersonModel[6500];
var something3 = new PersonModel[6500];
dataResults.CopyTo(something);
dataResults.CopyTo(something1);
dataResults.CopyTo(something2);
dataResults.CopyTo(something3);
var watch2 = System.Diagnostics.Stopwatch.StartNew();
parallelThreading.AsParallelAddingParallelization(something1.ToList());
watch2.Stop();
elapsedMs = watch2.ElapsedMilliseconds;
results.Add(nameof(parallelThreading.AsParallelAddingParallelization), elapsedMs);
var watch = System.Diagnostics.Stopwatch.StartNew();
parallelThreading.AsParallel(something2.ToList());
watch.Stop();
elapsedMs = watch.ElapsedMilliseconds;
results.Add(nameof(parallelThreading.AsParallel), elapsedMs);
var watch1 = System.Diagnostics.Stopwatch.StartNew();
parallelThreading.WithoutAsParallel(something3.ToList());
watch1.Stop();
elapsedMs = watch1.ElapsedMilliseconds;
results.Add(nameof(parallelThreading.WithoutAsParallel), elapsedMs);
var watch3 = System.Diagnostics.Stopwatch.StartNew();
parallelThreading.AsParallelAsOrdered(something.ToList());
watch3.Stop();
elapsedMs = watch3.ElapsedMilliseconds;
results.Add(nameof(parallelThreading.AsParallelAsOrdered), elapsedMs);
foreach (var result in results)
{
WriteTime(result.Key, result.Value);
}
Console.ReadLine();
}
private static void WriteTime(string methodName, long elapsedMiliseconds)
{
Console.WriteLine($" {methodName}: Milisecond passed: { elapsedMiliseconds}");
}
}
ParallelThreading.cs
public class ParallelThreading
{
private List<PersonModel> RandomData(int range)
{
List<PersonModel> personModels = new List<PersonModel>();
for (int i = 0; i < range; i++)
{
var person = new PersonModel()
{
NumberIdentification = i,
Age = Convert.ToInt32(GenerateString.RandomNumbers(2)),
DateOfBirth = GenerateString.GenerateDateTime(),
LastName = GenerateString.RandomString(10),
Name = GenerateString.RandomString(10),
City = GenerateString.RandomString(10)
};
personModels.Add(person);
}
return personModels;
}
private List<PersonModel> RandomDataWithSpecificSameData(int range, string city)
{
List<PersonModel> personModels = new List<PersonModel>();
for (int i = 0; i < range; i++)
{
var person = new PersonModel();
if (GenerateString.random.Next(range - 1) % 2 == 0)
{
person = new PersonModel()
{
NumberIdentification = i,
Age = Convert.ToInt32(GenerateString.RandomNumbers(2)),
DateOfBirth = GenerateString.GenerateDateTime(),
LastName = GenerateString.RandomString(10),
Name = GenerateString.RandomString(10),
City = city
};
}
else
{
person = new PersonModel()
{
NumberIdentification = i,
Age = Convert.ToInt32(GenerateString.RandomNumbers(2)),
DateOfBirth = GenerateString.GenerateDateTime(),
LastName = GenerateString.RandomString(10),
Name = GenerateString.RandomString(10),
City = GenerateString.RandomString(10),
};
}
personModels.Add(person);
}
return personModels;
}
#region AsParallelLINQ
public List<PersonModel> InitializeDataForParallelData(int range)
{
return RandomDataWithSpecificSameData(range, "Oradea");
}
public void AsParallel(List<PersonModel> data)
{
var result = data
.AsParallel()
.Where(c => c.City == "Oradea")
.Select(c => c);
foreach (var person in result)
{
Console.WriteLine($"{person.NumberIdentification} + {person.Name} + {person.City}");
}
}
public void AsParallelAddingParallelization(List<PersonModel> data)
{
var result = data
.AsParallel()
.WithDegreeOfParallelism(8)
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.Where(c => c.City == "Oradea")
.Select(c => c);
foreach (var person in result)
{
Console.WriteLine($"{person.NumberIdentification} + {person.Name} + {person.City}");
}
}
public void AsParallelAsOrdered(List<PersonModel> data)
{
var result = data
.AsParallel()
.AsOrdered()
.Where(c => c.City == "Oradea")
.Select(c => c);
foreach (var person in result)
{
Console.WriteLine($"{person.NumberIdentification} + {person.Name} + {person.City}");
}
}
public void WithoutAsParallel(List<PersonModel> data)
{
var result = data.Where(c => c.City == "Oradea").Select(c => c);
foreach (var person in result)
{
Console.WriteLine($"{person.NumberIdentification} + {person.Name} + {person.City}");
}
}
#endregion
}