<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>emphess .NET &#187; Snippets</title>
	<atom:link href="http://www.emphess.net/tag/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emphess.net</link>
	<description>Christoph Menge&#039;s Blog</description>
	<lastBuildDate>Tue, 15 Jun 2010 00:50:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A very simple profiler</title>
		<link>http://www.emphess.net/2009/03/04/a-very-simple-profiler/</link>
		<comments>http://www.emphess.net/2009/03/04/a-very-simple-profiler/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 20:27:01 +0000</pubDate>
		<dc:creator>Christoph Menge</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Profiler]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.emphess.net/?p=46</guid>
		<description><![CDATA[When I just posted the source code of the Dependency Property Serialization sample, I realized it included a small piece of source code that might come in handy from time to time:
A very basic, but rather exact profiler. It uses QueryPerformanceCounter. Perhaps I shouldn&#8217;t even call this profiler, because a profiler typically keeps track of [...]]]></description>
			<content:encoded><![CDATA[<p>When I just posted the source code of the <a href="http://www.emphess.net/2009/03/04/dependencyproperty-serialization-part-ii/">Dependency Property Serialization sample</a>, I realized it included a small piece of source code that might come in handy from time to time:</p>
<p>A very basic, but rather exact profiler. It uses <code>QueryPerformanceCounter</code>. Perhaps I shouldn&#8217;t even call this profiler, because a profiler typically keeps track of the parts of code it measures. So let&#8217;s call it a precise stopwatch. Well, anyways, here&#8217;s the code:</p>
<p><span id="more-46"></span></p>
<pre lang="csharp">
using System.Runtime.InteropServices;

namespace SimpleProfiler
{
    public class Profiler
    {
        long mFrequency;

        long mAccumulatedTime;
        long mStartTime;
        long mEndTime;

        long mLastTime;

        bool mInitialized = false;
        bool mRunning = false;

        public Profiler()
        {
            mInitialized = QueryPerformanceFrequency(out mFrequency);
        }

        public bool StartMeasure()
        {
            if (mInitialized &#038;&#038; !mRunning)
            {
                mRunning = true;
                return QueryPerformanceCounter(out mStartTime);
            }

            return false;
        }

        public bool StopMeasure()
        {
            if (mInitialized &#038;&#038; mRunning)
            {
                mRunning = false;
                bool bSuccess = QueryPerformanceCounter(out mEndTime);

                if (bSuccess)
                {
                    mLastTime = mEndTime - mStartTime;
                    mAccumulatedTime += mLastTime;
                    return true;
                }
            }

            return false;
        }

        public double GetLastTime()
        {
            return (double)((double)mLastTime / (double)mFrequency);
        }

        public double GetAccumulatedTime()
        {
            return (double)((double)mAccumulatedTime / (double)mFrequency);
        }

        [DllImport("kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long count);

        [DllImport("kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(out long frequency);
    }
}
</pre>
<p>which is also contained in the <a href="http://www.emphess.net/wp-content/uploads/2009/03/bpboserialization.zip">source code for the previous article</a>.</p>
<div style="float: right;"><p align="right"><a rel="nofollow" class="tt" href="http://twitter.com/home/?status=A+very+simple+profiler+http://bit.ly/cbNxVn" title="Post to Twitter"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/tt-twitter2.png" alt="Post to Twitter" /></a> <a rel="nofollow" class="tt" href="http://delicious.com/post?url=http://www.emphess.net/2009/03/04/a-very-simple-profiler/&amp;title=A+very+simple+profiler" title="Post to Delicious"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="Post to Delicious" /></a> <a rel="nofollow" class="tt" href="http://digg.com/submit?url=http://www.emphess.net/2009/03/04/a-very-simple-profiler/&amp;title=A+very+simple+profiler" title="Post to Digg"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="Post to Digg" /></a> <a rel="nofollow" class="tt" href="http://www.facebook.com/share.php?u=http://www.emphess.net/2009/03/04/a-very-simple-profiler/&amp;t=A+very+simple+profiler" title="Post to Facebook"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/tt-facebook.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.emphess.net/2009/03/04/a-very-simple-profiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
