Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#
Tip/Trick

Improving Execution Time with Code Tied to Nested States

Rate me:
Please Sign up or sign in to vote.
4.15/5 (10 votes)
2 Dec 2015CPOL2 min read 16.6K   6   11
We often end up having code that evaluates various states on a class, just to figure out what other code needs to execute. This little tip is something that we have implemented and we have been able to get noticeable improvements in execution time.

Introduction

Let's say we have a class with these three Attributes:

C#
public bool ValueA { get; set; }
public int ValueB { get; set; }
public string ValueC { get; set; }

Then, if the structure of the method below looks more or less familiar to you, then read on.

C#
public void DoStuff()
{
	if (ValueA)
	{
		if (ValueB < 100)
		{
			if (ValueC.Length > 10)
			{
				ASmallBLongC();
			}
			else
			{
				ASmallBShortC();
			}

		} 
		else
		{
			if (ValueC.Length > 10)
			{
				ALargeBLongC();
			}
			else
			{
				ALargeBShortC();
			}
		}
	}
	else
	{
		if (ValueB < 100)
		{
			if (ValueC.Length > 10)
			{
				NoASmallBLongC();
			}
			else
			{
				NoASmallBShortC();
			}
		}
		else
		{
			if (ValueC.Length > 10)
			{
				NoALargeBLongC();
			}
			else
			{
				NoALargeBShortC();
			}
		}
	}
}

The method shown here is an exposed method that figures out which exact method to call, based on the state of the class' attributes. More often than not, this type of code nests deeper than just the three attributes used in this example.

It is also quite often the case that the attributes get set fewer times than what the actual method executes for.

Background

After spending loads of time in code such as this to ensure that the flow logic is correct, we started thinking whether there isn't a more optimal way of coding this? Typically, in our use, we would create a class, set the Attributes and then get the class to perform a method (or a number of methods) many times over.

After a bit of experimentation, we opted to make use of a delegate that we assign once, only when any of the attributes change, and we were able to get the code to execute a bit faster.

The Solution

The improved solution saw us introduce a delegate such as this (one that would represent the signature of the method being called:

C#
public delegate void DoStuffDelegate();

Our DoStuff method was then changed to be a delegate, like this:

C#
public DoStuffDelegate DoStuff;

and our original DoStuff() method was renamed and changed to assign the delegate to the correct method, based on the values of the Attributes.

So we changed this:

C#
NoALargeBLongC();

to this:

C#
DoStuff = ASmallBLongC;

Our tests have shown that if the DoStuff gets called 10 million times, we get an execution time of 340 MS and using the revised class where we assign the delegate, this came down to 42 MS which is a noticeable difference.

Points of Interest

This is not something that will work for every case, I would recommend doing this only in cases where you set attributes of classes not that often, but you have code that forks out that has to be executed much more.

I have included a sample project with the two classes in it, so you can see the whole sample working. Happy to hear of any improvements that can be made.

History

  • 2nd December, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionStrategy Pattern implemented with Action?! Pin
Huh? Come Again?13-Dec-15 7:06
Huh? Come Again?13-Dec-15 7:06 
Generalinteresting concept - with some observations Pin
WilliamFalconerUK7-Dec-15 23:17
professionalWilliamFalconerUK7-Dec-15 23:17 
GeneralRe: interesting concept - with some observations Pin
FrancoisViljoen8-Dec-15 2:37
FrancoisViljoen8-Dec-15 2:37 
GeneralNot a bad idea Pin
L Viljoen3-Dec-15 2:07
professionalL Viljoen3-Dec-15 2:07 
GeneralRe: Not a bad idea Pin
FrancoisViljoen3-Dec-15 3:31
FrancoisViljoen3-Dec-15 3:31 
GeneralRe: Not a bad idea Pin
L Viljoen3-Dec-15 22:02
professionalL Viljoen3-Dec-15 22:02 
GeneralRe: Not a bad idea Pin
FrancoisViljoen4-Dec-15 6:53
FrancoisViljoen4-Dec-15 6:53 
GeneralRe: Not a bad idea Pin
L Viljoen4-Dec-15 7:57
professionalL Viljoen4-Dec-15 7:57 
GeneralRe: Not a bad idea Pin
L Viljoen3-Dec-15 22:03
professionalL Viljoen3-Dec-15 22:03 
GeneralRe: Not a bad idea Pin
FrancoisViljoen4-Dec-15 6:52
FrancoisViljoen4-Dec-15 6:52 
GeneralRe: Not a bad idea Pin
L Viljoen4-Dec-15 8:00
professionalL Viljoen4-Dec-15 8:00 
Small world indeed; all the best to you in cloudy UK
L Viljoen
Technical Lead / Architect .NET, Unity, C++

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.