Performance Check for Database Call C# before and after using AsParallel() (Not recommended for database call)
Brief
Today we will discuss the performance check for the below things.- OrderBy().ToList() : This is very slow.
- OrderBy().AsParallel().ToList(): This is very fast.
- AsParallel().OrderBy().ToList(): This is normal.
Console Application Code is as below:
public static void Main()
{
const int m = 10000;
{
const int m = 10000;
var s1 = Stopwatch.StartNew();
var students1 = context.Students.OrderBy(s => s.Name).ToList();
s1.Stop();
var students1 = context.Students.OrderBy(s => s.Name).ToList();
s1.Stop();
var s2 = Stopwatch.StartNew();
var students2 = context.Students.OrderBy(s => s.Name).AsParallel().ToList();
s2.Stop();
var students2 = context.Students.OrderBy(s => s.Name).AsParallel().ToList();
s2.Stop();
var s3 = Stopwatch.StartNew();
var students3 = context.Students.AsParallel().OrderBy(s => s.Name).ToList();
s3.Stop();
var students3 = context.Students.AsParallel().OrderBy(s => s.Name).ToList();
s3.Stop();
Console.WriteLine("Normal Fetch Time Students : " + ((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
m).ToString("0.00 ns"));
Console.WriteLine("Order By Then AsParallel Fetch Time Students : " + ((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
m).ToString("0.00 ns"));
Console.WriteLine("AsParallel Then Order By Fetch Time Students : " + ((double)(s3.Elapsed.TotalMilliseconds * 1000000) /
m).ToString("0.00 ns"));
}
m).ToString("0.00 ns"));
Console.WriteLine("Order By Then AsParallel Fetch Time Students : " + ((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
m).ToString("0.00 ns"));
Console.WriteLine("AsParallel Then Order By Fetch Time Students : " + ((double)(s3.Elapsed.TotalMilliseconds * 1000000) /
m).ToString("0.00 ns"));
}
Output
Note
The code of the context class and student class with the details of the database connection with fluent api can be find here.
Please refer this when to use AsParallel() and it's anomalies.
Please refer this when to use AsParallel() and it's anomalies.
Comments
Post a Comment