Memoization: Speed up expensive functions in ASP .NET
This article is about Optimizing ASP .NET server side execution using Memoization technique.
Before understanding Memoization, let’s discuss the simple but time consuming scenario first.
Consider the simple example in ASP .NET Web-form, I want to create Table control populated with list of students and bind that control to asp:panel. The reason to consider this example is it’s really simple but time consuming when data to be populated is increased. Obviously we can populate gridview which in-turn will be rendered as table, but to demonstrate the memoization I’m going to populate data manually.
Normally we will create Table WebControl and add TableRow WebControl for each student.
First we want simple student entity class,which is as follows.
Then added some method to get TableRow let’s say dataToRow, which is wrapped in HTMLtable class as follows.
As you can see dataToRow accepts single student object and returns TableRow with name and description of that student in different TableCell.
As this article is only to demonstrate Momoization, I’m going to create student object list by dumping 10000 of same data.Following function returns that list of student objects.
I have added Label on aspx page to display the time consumption result.And code-behind is as follows.
After running the result on webpage is as follows.
(NOTE: The result varies on each refresh but average time is 10-17 ms)
Some of you may argue that it’s quite fast as operation is done within 20 ms, so what’s need of optimizing this method. It may look negligible problem now but considering the big enterprise software or Web Application,where billions of data is to be processed this problem may look much bigger.
In above example, dataToRow is called for each record of student with same data, But even the same data is passed to method, it processes same operation each time and gives same result each time.
To optimize this type of operations we need to cache the functions, so when next time the function is called with parameter value it will return the result cached instead of recalculating or repeating the operations to get the result, This is called Memoization.
NOTE: I recommend to cache the function which are to much complex and time consuming and may be called by passing same parameters.
Let’s consider our example, now to use memoization create generic method which will accept the function.This generic method will check the dictionary for match, if found then the result will be returned without executing the passed function else the passed function will be executed and the result will be stored in dictionary before returning result.
As above function is generic, it will memoize any function having one parameter one and one return value. But for now let’s utilize above construction to optimize the dataToRow function and analyse the difference between normal processing technique and memoization technique.
Above I have processed the normal method and memoized method 5 times to multiple set of results to compare. And also calculated average times and ratio percentage at last. The result is as follows.
As you can see the optimized method is almost 10 time faster than optimized one, Interesting thing here is that the addition of all 5 optimized execution time is much less than lowest of all normal execution time.
I refreshed the page number of times and the ratio percentage never came down to even 700%. The reason is in normal execution the function executed each time, and in memoize technique the function only executed first time for above data.
Obviously, this exact scenario will never occurred in real Web Application development where same data will be processed thousand time in same way. But still using this technique in certain cases will optimize the process not 1000% or 8000% but 150-200% increase in performance can be definitely achieved.
If I have missed something or something wrong found please let me know, This will help to increase my knowledge.
Thanks for reading, If you have suggestions/queries comment below. Share this article to friend.
Before understanding Memoization, let’s discuss the simple but time consuming scenario first.
Consider the simple example in ASP .NET Web-form, I want to create Table control populated with list of students and bind that control to asp:panel. The reason to consider this example is it’s really simple but time consuming when data to be populated is increased. Obviously we can populate gridview which in-turn will be rendered as table, but to demonstrate the memoization I’m going to populate data manually.
Normally we will create Table WebControl and add TableRow WebControl for each student.
First we want simple student entity class,which is as follows.
public class Student { public string name { get; set; } public string description { get; set;} }
Then added some method to get TableRow let’s say dataToRow, which is wrapped in HTMLtable class as follows.
public class HTMLtable { TableRow tr; TableCell td1; TableCell td2; public TableRow dataToRow(Student stud) { tr = new TableRow(); td1 = new TableCell(); td1.Text = stud.name; tr.Cells.Add(td1); td2 = new TableCell(); td2.Text = stud.description; tr.Cells.Add(td2); return tr; } }
As you can see dataToRow accepts single student object and returns TableRow with name and description of that student in different TableCell.
As this article is only to demonstrate Momoization, I’m going to create student object list by dumping 10000 of same data.Following function returns that list of student objects.
private List getStudent() { var studentList = new List(); var student = new Student(); for (int i = 0; i < 10000; i++) { student.name = "student"; student.description = "description"; studentList.Add(student); } return studentList; }
I have added Label on aspx page to display the time consumption result.And code-behind is as follows.
After running the result on webpage is as follows.
(NOTE: The result varies on each refresh but average time is 10-17 ms)
Some of you may argue that it’s quite fast as operation is done within 20 ms, so what’s need of optimizing this method. It may look negligible problem now but considering the big enterprise software or Web Application,where billions of data is to be processed this problem may look much bigger.
In above example, dataToRow is called for each record of student with same data, But even the same data is passed to method, it processes same operation each time and gives same result each time.
To optimize this type of operations we need to cache the functions, so when next time the function is called with parameter value it will return the result cached instead of recalculating or repeating the operations to get the result, This is called Memoization.
Memoization is technique to cache the function result and return result from cache if same request is made.
NOTE: I recommend to cache the function which are to much complex and time consuming and may be called by passing same parameters.
Let’s consider our example, now to use memoization create generic method which will accept the function.This generic method will check the dictionary for match, if found then the result will be returned without executing the passed function else the passed function will be executed and the result will be stored in dictionary before returning result.
As above function is generic, it will memoize any function having one parameter one and one return value. But for now let’s utilize above construction to optimize the dataToRow function and analyse the difference between normal processing technique and memoization technique.
As you can see the optimized method is almost 10 time faster than optimized one, Interesting thing here is that the addition of all 5 optimized execution time is much less than lowest of all normal execution time.
I refreshed the page number of times and the ratio percentage never came down to even 700%. The reason is in normal execution the function executed each time, and in memoize technique the function only executed first time for above data.
Obviously, this exact scenario will never occurred in real Web Application development where same data will be processed thousand time in same way. But still using this technique in certain cases will optimize the process not 1000% or 8000% but 150-200% increase in performance can be definitely achieved.
If I have missed something or something wrong found please let me know, This will help to increase my knowledge.
Thanks for reading, If you have suggestions/queries comment below. Share this article to friend.
Comments
Post a Comment