Measuring class load times with utility method [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
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.
c# performance .net
closed as off-topic by t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean Jan 26 at 13:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean
add a comment |Â
up vote
-1
down vote
favorite
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.
c# performance .net
closed as off-topic by t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean Jan 26 at 13:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
2
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 at 5:49
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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.
c# performance .net
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.
c# performance .net
asked Jan 25 at 1:42
ropuxil
343
343
closed as off-topic by t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean Jan 26 at 13:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean
closed as off-topic by t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean Jan 26 at 13:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." â t3chb0t, Sam Onela, Graipher, Pieter Witvoet, Donald.McLean
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
2
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 at 5:49
add a comment |Â
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
2
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 at 5:49
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
2
2
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 at 5:49
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 at 5:49
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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>(_ =>
_.Load("resources/config/server.config.json")
);
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>(_ =>
_.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@ropuxil You are using the async version. That will work if you are usingTask
. Use the first suggestion. The second one is if you are using async/await syntax.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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>(_ =>
_.Load("resources/config/server.config.json")
);
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>(_ =>
_.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@ropuxil You are using the async version. That will work if you are usingTask
. Use the first suggestion. The second one is if you are using async/await syntax.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
add a comment |Â
up vote
2
down vote
accepted
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>(_ =>
_.Load("resources/config/server.config.json")
);
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>(_ =>
_.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@ropuxil You are using the async version. That will work if you are usingTask
. Use the first suggestion. The second one is if you are using async/await syntax.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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>(_ =>
_.Load("resources/config/server.config.json")
);
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>(_ =>
_.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
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>(_ =>
_.Load("resources/config/server.config.json")
);
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>(_ =>
_.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
edited Jan 25 at 2:07
answered Jan 25 at 1:50
Nkosi
1,870619
1,870619
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@ropuxil You are using the async version. That will work if you are usingTask
. Use the first suggestion. The second one is if you are using async/await syntax.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
add a comment |Â
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@ropuxil You are using the async version. That will work if you are usingTask
. Use the first suggestion. The second one is if you are using async/await syntax.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png
â ropuxil
Jan 25 at 2:04
@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 at 2:07
@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 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
@ropuxil I've renamed the second one to avoid any confusion.
â Nkosi
Jan 25 at 2:07
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
Would changing my methods to async and using the second one be an improvement over the first one?
â ropuxil
Jan 25 at 2:11
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang.
â Nkosi
Jan 25 at 2:13
add a comment |Â
Create a delegate that can be executed after initializing the class.
â Nkosi
Jan 25 at 1:48
A code example would help with this, I'm not entirely sure of your concept.
â ropuxil
Jan 25 at 1:48
2
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 at 5:49