Author Topic: TestTimer  (Read 1470 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
TestTimer
« on: October 22, 2012, 12:50:53 PM »
This is a simple but useful idea from Lee Campbell's free ebook Introduction to Rx
 
Maybe should add logic for adding in Editor's EnteringQuiescentState event as pointed out by Spiderinnet1 at AutoCAD .NET: Real Performance of ObjectId.Open/GetObject Transaction and OpenCloseTransaction
 
Also adding logic for nesting might be useful?
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.AutoCAD.ApplicationServices.Core;
  8. namespace Autodesk.AutoCAD.Diagnostics
  9. {
  10.     /// <summary>
  11.     /// Author : Lee Campbell
  12.     /// Source : http://www.introtorx.com/
  13.     /// Modified to print to AutoCAD console.
  14.     /// </summary>
  15.     public class TestTimer : IDisposable
  16.     {
  17.         private readonly string _name;
  18.         private readonly Stopwatch _watch;
  19.         public TestTimer(string name)
  20.         {
  21.             _name = name;
  22.             _watch = Stopwatch.StartNew();
  23.            
  24.         }
  25.         public void Dispose()
  26.         {
  27.             _watch.Stop();
  28.             Application.DocumentManager.MdiActiveDocument.Editor.WriteLine("{0} took {1}", _name, _watch&#8204;.Elapsed);
  29.         }
  30.     }
  31. }
  32.  

bargool

  • Guest
Re: TestTimer
« Reply #1 on: October 23, 2012, 03:02:13 AM »
Is useful something like this?
Code - C#: [Select]
  1. public class TestTimer : IDisposable
  2. {
  3.         private readonly string _name;
  4.         private readonly Stopwatch _watch;
  5.         private readonly Action<string> _action;
  6.        
  7.         public TestTimer(string name)
  8.         {
  9.                 _name = name;
  10.                 _watch = Stopwatch.StartNew();
  11.         }
  12.        
  13.         public TestTimer(string name, Action<string> action)
  14.                 : this(name)
  15.         {
  16.                 _action = action;
  17.         }
  18.        
  19.         public void Dispose()
  20.         {
  21.                 _watch.Stop();
  22.                 string message = string.Format("{0} took {1}", _name, _watch.Elapsed);
  23.                 if (_action == null)
  24.                         Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n"+message);
  25.                 else
  26.                         _action(message);
  27.         }
  28. }

Code - C#: [Select]
  1. using (TestTimer timer = new TestTimer("Creating 1000 random lines", n => log.AppendLine(n)))
  2. {
  3. ...
  4. }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: TestTimer
« Reply #2 on: October 23, 2012, 10:05:57 AM »
Yes Bargool that would be one way.