# Measuring class load times with utility method [closed]

When ever I load a new class I measure the time it takes to setup, initializing and load that class, it helps me debug the time it takes to complete actions within my application.

Here I have a pretty nice feature that does it all for me.

public static T CreateInstanceOf<T>() where T : new()
{
var stopwatch = Stopwatch.StartNew();
var result = new T();

stopwatch.Stop();
Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

return result;
}


It also has really simple usage, like so..

ConfigHandler = CoreUtilities.CreateInstanceOf<ConfigHandler>();


But, the issues come when I want to load the class. Now you could say that I could put this in the constructor and have my above method measure the time the method takes as well as initiating the class, but as C# has stated, constructors aren't for calling methods.

While I want to measure the time effiently, I also don't want to break any language rules that are recommended to follow, can anyone think of a work around.

Here is all I do to load the classes, just a simple method.

ConfigHandler.Load("resources/config/server.config.json");


I could just add a stopwatch between every 2 lines of everything I initialize, but lets be honest, who wants to do that, especially when you have around 30 classes to initiate in your project.

• Create a delegate that can be executed after initializing the class. – Nkosi Jan 25 '18 at 1:48
• A code example would help with this, I'm not entirely sure of your concept. – ropuxil Jan 25 '18 at 1:48
• can anyone think of a work around for sure but you did not implement it yourself yet and Code Review is not a give meh teh codez site but an improve meh codez one thus I'm voting to close for code not yet written. – t3chb0t Jan 26 '18 at 5:49

Create a delegate that can be executed after initializing the class.

public static T CreateInstanceOf<T>(Action<T> configure = null) where T : new() {
var stopwatch = Stopwatch.StartNew();

var result = new T();

if(configure != null) {
configure(result);
}

stopwatch.Stop();
Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

return result;
}


and used

var handler = CoreUtilities.CreateInstanceOf<ConfigHandler>(_ =>
);


Having the optional parameter means your original use case still applies

CoreUtilities.CreateInstanceOf<ConfigHandler>(); //parameter defaults to null


Room for possible improvements like async overload as well

public static async Task<T> CreateInstanceOfAsync<T>(Func<T, Task> configure = null) where T : new() {
var stopwatch = Stopwatch.StartNew();

var result = await Task.Run(() => return new T());
if(configure != null) {
await configure(result);
}

stopwatch.Stop();
Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

return result;
}


used like

var handler = await CoreUtilities.CreateInstanceOf<ConfigHandler>(_ =>

• @ropuxil You are using the async version. That will work if you are using Task. Use the first suggestion. The second one is if you are using async/await syntax. – Nkosi Jan 25 '18 at 2:07