3
\$\begingroup\$

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]");
\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 17, 2017 at 13:57
  • 2
    \$\begingroup\$ Why does your code have so many Managers? \$\endgroup\$ Commented Mar 17, 2017 at 14:01
  • \$\begingroup\$ I have about 100 of these to load - There's something terribly wrong with your implementation. \$\endgroup\$
    – t3chb0t
    Commented Mar 20, 2017 at 7:22

1 Answer 1

1
\$\begingroup\$

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...
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented 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
    Commented 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\$ Commented Mar 17, 2017 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.