Main Site Documentation

Simple code profiler


#1

I’m tuning some GPS parsing code (see ). I believe you can’t do performance tuning without some kind of benchmarking. I wrote this simple class that lets me time sections of code. It could be much more elaborate, but this seems to work.

The timer can be embedded in loops or events; the class keeps a running elapsed time since it was created and how much time was spent in the timed section. It reports to the Debug console at an interval you define.

Usage:


class MyClass
{
    Utility.PerfTimer myProcTimer = new Utility.PerfTimer("Nmea GPS recv data", 
                    new TimeSpan(0,0,10));

    void MyMethod()
    {
        // do some stuff
        myProcTimer .Start();
        // time this stuff
        myProcTimer .Stop();
     }


Every 10 seconds, this appears in the debug window:
Timer Nmea GPS recv data elapsed: 9400062; timed=1019556; percent: 10.846

So this tells me that just over 10% of the running time in this program is spent in the timed section. This isn’t smart about interrupts, threads, etc, so heavy uses of these will skew the numbers. But everthing else being the same, you can see the impact of changes you make to your code.

Here’s the code:


        /// <summary>
        /// Used to time the time spend in areas of code as a portion of elapsed time. Call Start() and Stop() to mark the code sections being timed.
        /// Set ReportInterval to get timed reports;
        /// </summary>
        public class PerfTimer
        {
            DateTime elapsedStarted;
            DateTime timerStarted = DateTime.MaxValue;
            TimeSpan elapsed = TimeSpan.Zero;
            string name;

            TimeSpan reportingInterval = TimeSpan.Zero;
            Timer reportTimer;

            public PerfTimer(string name, TimeSpan reportInterval)
            {
                this.name = name;
                elapsedStarted = DateTime.Now;
                this.ReportingInterval = reportInterval;
            }
            public PerfTimer(string name) : this(name, TimeSpan.Zero)
            {                
            }

            public void Start()
            {
                timerStarted = DateTime.Now;
            }

            public void Stop()
            {
                if (timerStarted != DateTime.MaxValue)
                {
                    elapsed += (DateTime.Now - timerStarted);
                    timerStarted = DateTime.MaxValue;
                }
            }

            public TimeSpan ReportingInterval
            {
                get
                {
                    return reportingInterval;
                }
                set
                {
                    if (reportTimer != null)
                    {
                        reportTimer.Dispose();
                    }
                    if (value != TimeSpan.Zero)
                    {
                        reportTimer = new Timer(new TimerCallback(Report), null, value, value);
                       
                    }
                    reportingInterval = value;
                }

            }

            public void Report(object state)
            {
                long ticksElapsed = (DateTime.Now - elapsedStarted).Ticks ;
                long ticksTimed = elapsed.Ticks;
                Debug.Print("Timer " + name + " elapsed: " + ticksElapsed / 1000 + "; timed=" + ticksTimed / 1000 + "; percent: " + ((((float)ticksTimed) / ((float)ticksElapsed)) * 100).ToString("f3"));
            }


        }


#2

Fezzer.com?


#3

Absolutely… after fezzer.com’s ‘create’ function is back online. :slight_smile:


#4

Posted: http://www.fezzer.com/project/129/simple-code-performance-profiler/

:smiley:


#5

:slight_smile: you beat me to the clock. I posted MF version of .Net’s Stopwatch class here:
http://www.fezzer.com/project/133/stopwatch/


#6

ah, very similar. StopWatch gives you more control and lets you reset. Mine has the periodic reporting built in. Perhaps combing the two would be useful.