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]");

// 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]");

• Where is this code? In a constructor? Are the fields readonly? Can the constructor be parameterized? How close to the entry point is it? Who is the caller? – Mathieu Guindon Mar 17 '17 at 13:57
• Why does your code have so many Managers? – 200_success Mar 17 '17 at 14:01
• I have about 100 of these to load - There's something terribly wrong with your implementation. – t3chb0t Mar 20 '17 at 7:22

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>();

• I don't know where you're getting Stopwatch.StartNew(), but if it has stopwatch.Pause() and stopwatch.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. – Behnam Rasooli Mar 17 '17 at 13:22