\$\begingroup\$
\$\endgroup\$
3
I was just wondering, is there a way to load these better and log the times they each took? It seems like a headache having to write about 90+ stopwatches, which is what I will have to do as I have about 100 of these to load.
// PacketManager
var packetManagerStopwatch = Stopwatch.StartNew();
_packetManager = new PacketManager();
packetManagerStopwatch.Stop();
Logger.Trace("Loaded PacketManager [took " + packetManagerStopwatch.ElapsedMilliseconds + "ms]");
// Achievements
var achievementStopwatch = Stopwatch.StartNew();
_achievementManager = new AchievementManager();
achievementStopwatch.Stop();
Logger.Trace("Loaded Achievements [took " + achievementStopwatch.ElapsedMilliseconds + "ms]");
// Talents
var talentsStopwatch = Stopwatch.StartNew();
_talentTrackManager = new TalentTrackManager();
talentsStopwatch.Stop();
Logger.Trace("Loaded Talents [took " + talentsStopwatch.ElapsedMilliseconds + "ms]");
// Badges
var badgeStopwatch = Stopwatch.StartNew();
_badgeManager = new BadgeManager();
badgeStopwatch.Stop();
Logger.Trace("Loaded Badges [took " + badgeStopwatch.ElapsedMilliseconds + "ms]");
// ClientManager
var clientManagerStopwatch = Stopwatch.StartNew();
_clientManager = new GameClientManager();
clientManagerStopwatch.Stop();
Logger.Trace("Loaded GameClientManager [took " + clientManagerStopwatch.ElapsedMilliseconds + "ms]");
// ModerationManager
var moderationManagerStopwatch = Stopwatch.StartNew();
_moderationManager = new ModerationManager();
moderationManagerStopwatch.Stop();
Logger.Trace("Loaded ModerationManager [took " + moderationManagerStopwatch.ElapsedMilliseconds + "ms]");
// ItemDataManager
var itemManagerStopwatch = Stopwatch.StartNew();
_itemDataManager = new ItemDataManager();
itemManagerStopwatch.Stop();
Logger.Trace("Loaded ItemDataManager [took " + itemManagerStopwatch.ElapsedMilliseconds + "ms]");
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Mar 17, 2017 at 12:20
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
You need a generic method like this:
static T createInstanceOf<T>() where T : new() {
var stopwatch = Stopwatch.StartNew();
T result = new T();
stopwatch.Stop();
Logger.Trace("Loaded "+ result.GetType().Name +" [took " + packetManagerStopwatch.ElapsedMilliseconds + "ms]");
return result;
}
Now you can simply use it like this:
_packetManager = createInstanceOf<PacketManager>();
_achievementManager = createInstanceOf<AchievementManager>();
_talentTrackManager = createInstanceOf<TalentTrackManager>();
_badgeManager = createInstanceOf<BadgeManager>();
// and so on...
200_success
146k22 gold badges190 silver badges478 bronze badges
answered Mar 17, 2017 at 13:13
-
\$\begingroup\$ I don't know where you're getting
Stopwatch.StartNew()
, but if it hasstopwatch.Pause()
andstopwatch.Resume()
methods, it's better to create only one instance of it. The way you're doing now is creating a lot of work for the garbage collector. \$\endgroup\$Behnam Rasooli– Behnam Rasooli2017年03月17日 13:22:24 +00:00Commented Mar 17, 2017 at 13:22 -
1\$\begingroup\$ I like the general idea but (a) methods in c# are PascalCase, and (b) I don't like that it's doing all that extra stuff in a method that's only supposed to create an instance of something. Maybe for this case it's ok, but I think for production use you wouldn't be doing all this timing and tracing, so I'd wrap it in a decorator that calls the "create instance" method and times/logs it. \$\endgroup\$404– 4042017年03月17日 13:27:05 +00:00Commented Mar 17, 2017 at 13:27
-
\$\begingroup\$ For production use I would use a profiler tool, I wouldn't roll my own implementation. How would you use a decorator? \$\endgroup\$Behnam Rasooli– Behnam Rasooli2017年03月17日 13:35:37 +00:00Commented Mar 17, 2017 at 13:35
lang-cs
readonly
? Can the constructor be parameterized? How close to the entry point is it? Who is the caller? \$\endgroup\$