Last active
December 21, 2021 10:09
-
-
Save ambitiousrahul/802c5c14b938011c2bc81a15fd3abb8f to your computer and use it in GitHub Desktop.
Lazy Loading Techniques in C# dotnet core.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace LazyLoadingExample | |
{ | |
public class MyPreferences | |
{ | |
private Widget myWidget = null; | |
public Widget GetMyWidgetObject(int widgetId) | |
{ | |
myWidget = new Widget(widgetId); | |
return myWidget; | |
} | |
public Widget MyWidget => myWidget; | |
} | |
public class MyPreferencesLazyLoad | |
{ | |
private readonly Lazy<Widget> myLazyWidgetObj; | |
//default constructor | |
public MyPreferencesLazyLoad()=> myLazyWidgetObj = new Lazy<Widget>(); | |
//parameterized constructor. | |
public MyPreferencesLazyLoad(int widgetId) => myLazyWidgetObj = new Lazy<Widget>(() => new Widget(widgetId)); | |
public bool IsWidgetObjectCreated | |
{ | |
get | |
{ | |
if (myLazyWidgetObj != null) | |
{ | |
return myLazyWidgetObj.IsValueCreated; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
public Widget MyWidget => myLazyWidgetObj?.Value; | |
} | |
public class Widget | |
{ | |
private int _widgetId; | |
public Widget() { } | |
public Widget(int id) => _widgetId = id; | |
public void SetWidgetId(int widgetId) => _widgetId = widgetId; | |
public int GetWidgetId() => _widgetId; | |
} | |
class Program | |
{ | |
const int _maxLoppingNumber = 100000; | |
static void Main(string[] args) | |
{ | |
var s1 = Stopwatch.StartNew(); | |
var myPreferences = new MyPreferences(); | |
Console.WriteLine("---------------------Start 1st Loop Type-----------------------"); | |
Console.WriteLine("-------------Version 1: Lazy Loading old classic way--------------"); | |
Console.WriteLine(); | |
for (int i = 0; i < _maxLoppingNumber; i++) | |
{ | |
var widget = myPreferences.GetMyWidgetObject(i); | |
//print on every ten thousand loop | |
if ((i / 10000) >= 1 && (i % 10000) == 0) | |
{ | |
Console.WriteLine("Instance Count :- {0}", widget.GetWidgetId()); | |
} | |
} | |
s1.Stop(); | |
var lazyLoadMyPreferences = new MyPreferencesLazyLoad(); | |
Console.WriteLine(); | |
Console.WriteLine("Lazy object benefit"); | |
Console.WriteLine("Lazy Widget Object created : {0}", lazyLoadMyPreferences.IsWidgetObjectCreated); | |
Console.WriteLine("WidgetId to be created: {0}",lazyLoadMyPreferences.MyWidget.GetWidgetId()); | |
Console.WriteLine("Lazy Widget Object created : {0}", lazyLoadMyPreferences.IsWidgetObjectCreated); | |
Console.WriteLine(); | |
// Version 2: create Lazy every time in loop, and access Value each time. | |
var s2 = Stopwatch.StartNew(); | |
Console.WriteLine("-------Start 2nd Loop Type- Creating Lazy Loading object fresh in each iteration------"); | |
Console.WriteLine(); | |
for (int i = 0; i < _maxLoppingNumber; i++) | |
{ | |
var objMyPreferences = new MyPreferencesLazyLoad(i); | |
//print on every ten thousand loop | |
if ((i / 10000) >= 1 && (i % 10000) == 0) | |
{ | |
Console.WriteLine("Instance Count :- {0}", objMyPreferences.MyWidget.GetWidgetId()); | |
} | |
} | |
s2.Stop(); | |
Console.WriteLine(); | |
// Version 3 : create lazy object once , and access Value each time in loop. | |
var s3 = Stopwatch.StartNew(); | |
Console.WriteLine("------Start 3rd Loop Type- Creating Lazy Loading object once, access Value in each iteration------"); | |
Console.WriteLine(); | |
var objPreferences = new MyPreferencesLazyLoad(); | |
for (int i = 0; i < _maxLoppingNumber; i++) | |
{ | |
objPreferences.MyWidget.SetWidgetId(i); | |
//print on every ten thousand loop | |
if ((i / 10000) >= 1 && (i % 10000) == 0) | |
{ | |
Console.WriteLine("Instance Count :- {0}", objPreferences.MyWidget.GetWidgetId()); | |
} | |
} | |
s3.Stop(); | |
Console.WriteLine(); | |
Console.WriteLine("=======RESULTS BENCHMARKED======="); | |
Console.WriteLine(); | |
Console.WriteLine("performance for Classic Lazy Loading Approach: {0}", ((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _maxLoppingNumber).ToString("0.00 ns")); | |
Console.WriteLine("performance after Lazy loading object creation in every iteration: {0}", ((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _maxLoppingNumber).ToString("0.00 ns")); | |
Console.WriteLine("performance after reusing lazy loading object created once: {0}", ((double)(s3.Elapsed.TotalMilliseconds * 1000000) / _maxLoppingNumber).ToString("0.00 ns")); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment