Millions Of Objects Performance In C# WPF | Amit Padhiyar | Saatody
This is little experiment about memory management and time management in WPF while we create millions of objects in small amount of time. Let's see first example while we create one simple class with no properties (empty class). Before we start, we must define some properties in MainWindow.
private readonly int TotalObjects = 10000000;
private int i = 0;
private List<PointClass> Objs = new List<PointClass>();
private Stopwatch Timer = new Stopwatch();
private TimeSpan TS = new TimeSpan();
TotalObjects is variable. And this number used in loop for repeating same process. It means we will create PointClass's object in each cycle and store in Objs List<>. The 'i' is index for loop. Using Stopwatch we will calculate the time and store elapsed time in TimeSpan TS. This experiment uses two loop; 1] for loop and 2] while loop.
public class PointClass
{
}
First create empty class. This class hasn't any property. See the below example to see How much time its take to create 10,000,000 (10 Millions) objects.
XAML - We Don't Needed Right Now.
<Window x:Class="MillionsOfGraphicsObjects.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MillionsOfGraphicsObjects" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid Name="Container"> </Grid> </Window>
C# Code
using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows; using System.Windows.Shapes; namespace MillionsOfGraphicsObjects { public class PointClass { } public partial class MainWindow : Window { private readonly int TotalObjects = 10000000; private int i = 0; private List<PointClass> Objs = new List<PointClass>(); private Stopwatch Timer = new Stopwatch(); private TimeSpan TS = new TimeSpan(); public MainWindow() { InitializeComponent(); this.Loaded += (sender, e) => { i = 0; Objs.Clear(); Timer.Start(); for (i = 0; i < TotalObjects; i++) { PointClass Obj = new PointClass(); Objs.Add(Obj); } TS = Timer.Elapsed; PrintTimer(TS); Timer.Reset(); i = 0; Objs.Clear(); Timer.Start(); while (i < TotalObjects) { PointClass Obj = new PointClass(); Objs.Add(Obj); i++; } TS = Timer.Elapsed; PrintTimer(TS); Timer.Reset(); }; } private void PrintTimer(TimeSpan Time) { System.Console.WriteLine("Execution Time: " + String.Format("{00:00}.{01:000}", Time.Seconds, Time.Milliseconds)); } } }
Output
Execution Time: 00.973
Execution Time: 00.884
It's amazing. I created 10 million objects less than 1 second. Now we set one integer property in PointClass class. Now see what happen in below example.
public class PointClass
{
public int Value1 = 0;
}
After changes happen in PointClass class, The output will match like below output. But your computer performance might be different than my computer.
Execution Time: 00.956
Execution Time: 00.867
Here we got an output and there is not much difference between two outputs. Now add 50 integer objects in PointClass class. Then see what happen.
public class PointClass
{
public int Value1 = 0;
public int Value2 = 0;
public int Value3 = 0;
public int Value4 = 0;
public int Value5 = 0;
public int Value6 = 0;
public int Value7 = 0;
public int Value8 = 0;
public int Value9 = 0;
public int Value10 = 0;
public int Value11 = 0;
public int Value12 = 0;
public int Value13 = 0;
public int Value14 = 0;
public int Value15 = 0;
public int Value16 = 0;
public int Value17 = 0;
public int Value18 = 0;
public int Value19 = 0;
public int Value20 = 0;
public int Value21 = 0;
public int Value22 = 0;
public int Value23 = 0;
public int Value24 = 0;
public int Value25 = 0;
public int Value26 = 0;
public int Value27 = 0;
public int Value28 = 0;
public int Value29 = 0;
public int Value30 = 0;
public int Value31 = 0;
public int Value32 = 0;
public int Value33 = 0;
public int Value34 = 0;
public int Value36 = 0;
public int Value37 = 0;
public int Value38 = 0;
public int Value39 = 0;
public int Value40 = 0;
public int Value41 = 0;
public int Value42 = 0;
public int Value43 = 0;
public int Value44 = 0;
public int Value45 = 0;
public int Value46 = 0;
public int Value47 = 0;
public int Value48 = 0;
public int Value49 = 0;
public int Value50 = 0;
}
Execution Time: 06.643
Execution Time: 06.684
Here we got some difference between this output and above two outputs. It almost 5 seconds difference. Let's see, Now we create 10 million objects of Polygon class which is derived from System.Windows.Shapes namespace.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Shapes;
namespace MillionsOfGraphicsObjects
{
public partial class MainWindow : Window
{
private readonly int TotalObjects = 10000000;
private int i = 0;
private List<Polygon> Objs = new List<Polygon>();
private Stopwatch Timer = new Stopwatch();
private TimeSpan TS = new TimeSpan();
public MainWindow()
{
InitializeComponent();
this.Loaded += (sender, e) =>
{
i = 0;
Objs.Clear();
Timer.Start();
for (i = 0; i < TotalObjects; i++)
{
Polygon Obj = new Polygon();
Objs.Add(Obj);
}
TS = Timer.Elapsed;
PrintTimer(TS);
Timer.Reset();
i = 0;
Objs.Clear();
Timer.Start();
while (i < TotalObjects)
{
Polygon Obj = new Polygon();
Objs.Add(Obj);
i++;
}
TS = Timer.Elapsed;
PrintTimer(TS);
Timer.Reset();
};
}
private void PrintTimer(TimeSpan Time)
{
System.Console.WriteLine("Execution Time: " + String.Format("{00:00}.{01:000}", Time.Seconds, Time.Milliseconds));
}
}
}
Output
Execution Time: 09.878
Execution Time: 12.170
This is almost take double time than previous output. So the conclusion is that WPF Polygon objects are too heavy to store in memory and it's take too much time to create 10 Millions objects. And this is not good for heavy performance WPF software application.
We will try to find some efficient solutions to deal with this kind of problems.
Comments
Post a Comment