<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jeremy Simmons: The Blog - CSharp</title>
    <link>http://www.jeremysimmons.net/blog/</link>
    <description>Four out of Five Coders recommend</description>
    <language>en-us</language>
    <copyright>Jeremy Simmons 2005-2009</copyright>
    <lastBuildDate>Wed, 23 Jan 2008 23:49:32 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jsimmons@jeremysimmons.net</managingEditor>
    <webMaster>jsimmons@jeremysimmons.net</webMaster>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I want to get a Process ID for the current Application in C#, so which is faster (and
does it really matter).
</p>
        <p>
Lets find out!
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> ProcessIDFaceoff<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br />
[System.Runtime.InteropServices.DllImport(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"kernel32.dll"</span>,
SetLastError <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">true</span>)]<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">extern</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> GetCurrentProcessId();<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 3; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Program().RunTest();<br />
}<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> RunTest()<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">const</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> times <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1000;<br />
System.Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"PInvoke,Native"</span>);<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> j <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
j &lt; 20; j++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> DisposableStopwatch(System.Console.Out))<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; times; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> pid <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> GetCurrentProcessId();<br />
}<br />
}<br />
System.Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">","</span>);<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> DisposableStopwatch(System.Console.Out))<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; times; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> pid <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> System.Diagnostics.Process.GetCurrentProcess().Id;<br />
}<br />
}<br />
System.Console.WriteLine();<br />
}<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> DisposableStopwatch
: System.IDisposable<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> System.Diagnostics.Stopwatch
watch;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> DisposableStopwatch(System.IO.TextWriter
logger)<br />
{<br />
watch <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> System.Diagnostics.Stopwatch.StartNew();<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IDisposable
Members<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Dispose()<br />
{<br />
watch.Stop();<br />
System.Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"{0}"</span>,
watch.Elapsed);<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><br />
}<br />
}<br />
}<br /></span>
        </p>
        <p>
Yields something like this.
</p>
        <pre>PInvoke,Native
00:00:00.0003477,00:00:00.0004451
00:00:00.0000705,00:00:00.0003653
00:00:00.0000675,00:00:00.0003550
00:00:00.0000677,00:00:00.0003782
00:00:00.0000679,00:00:00.0003586
00:00:00.0000674,00:00:00.0003772
00:00:00.0000677,00:00:00.0017131
00:00:00.0000706,00:00:00.0004838
00:00:00.0000689,00:00:00.0053451
00:00:00.0000676,00:00:00.0003648
00:00:00.0000677,00:00:00.0003676
00:00:00.0000677,00:00:00.0003659
00:00:00.0000677,00:00:00.0017793
00:00:00.0000766,00:00:00.0004811
00:00:00.0000694,00:00:00.0004966
00:00:00.0000690,00:00:00.0004918
00:00:00.0000704,00:00:00.0003739
00:00:00.0000677,00:00:00.0003609
00:00:00.0000769,00:00:00.0003749
00:00:00.0000677,00:00:00.0034804
PInvoke,Native
00:00:00.0000704,00:00:00.0004326
00:00:00.0000695,00:00:00.0014194
00:00:00.0000770,00:00:00.0002896
00:00:00.0000681,00:00:00.0003052
00:00:00.0000705,00:00:00.0003009
00:00:00.0000711,00:00:00.0003472
00:00:00.0000708,00:00:00.0038629
00:00:00.0000681,00:00:00.0003193
00:00:00.0000678,00:00:00.0003672
00:00:00.0000679,00:00:00.0003150
00:00:00.0000675,00:00:00.0003610
00:00:00.0000679,00:00:00.0003227
00:00:00.0000674,00:00:00.0061228
00:00:00.0000704,00:00:00.0004289
00:00:00.0000701,00:00:00.0004732
00:00:00.0000696,00:00:00.0004107
00:00:00.0000697,00:00:00.0003274
00:00:00.0000678,00:00:00.0003034
00:00:00.0000679,00:00:00.0004169
00:00:00.0000688,00:00:00.0047879
PInvoke,Native
00:00:00.0000703,00:00:00.0004419
00:00:00.0000697,00:00:00.0004428
00:00:00.0000699,00:00:00.0003065
00:00:00.0000678,00:00:00.0003064
00:00:00.0000674,00:00:00.0003073
00:00:00.0000674,00:00:00.0015432
00:00:00.0000694,00:00:00.0004473
00:00:00.0000704,00:00:00.0053116
00:00:00.0000680,00:00:00.0003242
00:00:00.0000698,00:00:00.0003293
00:00:00.0000721,00:00:00.0003163
00:00:00.0000720,00:00:00.0003264
00:00:00.0000716,00:00:00.0039090
00:00:00.0000700,00:00:00.0004544
00:00:00.0000703,00:00:00.0003519
00:00:00.0000678,00:00:00.0003039
00:00:00.0000677,00:00:00.0003300
00:00:00.0000678,00:00:00.0003068
00:00:00.0000677,00:00:00.0070403
00:00:00.0000727,00:00:00.0003814</pre>
        <p>
Looks faster to me. By at least 3x-5x in most cases. That's maybe worth the consideration
to PInvoke it.
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0" />
      </body>
      <title>To PInvoke or To Native code, that is the question.</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2008/01/23/ToPInvokeOrToNativeCodeThatIsTheQuestion.aspx</link>
      <pubDate>Wed, 23 Jan 2008 23:49:32 GMT</pubDate>
      <description>&lt;p&gt;
I want to get a Process ID for the current Application in C#, so which is faster (and
does it really matter).
&lt;/p&gt;
&lt;p&gt;
Lets find out!
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; ProcessIDFaceoff&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
[System.Runtime.InteropServices.DllImport(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"kernel32.dll"&lt;/span&gt;,
SetLastError &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;true&lt;/span&gt;)]&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;extern&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; GetCurrentProcessId();&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 3; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Program().RunTest();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; RunTest()&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;const&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; times &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 1000;&lt;br&gt;
System.Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"PInvoke,Native"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; j &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
j &amp;lt; 20; j++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; DisposableStopwatch(System.Console.Out))&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; times; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; pid &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetCurrentProcessId();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
System.Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;","&lt;/span&gt;);&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; DisposableStopwatch(System.Console.Out))&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; times; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; pid &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; System.Diagnostics.Process.GetCurrentProcess().Id;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
System.Console.WriteLine();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; DisposableStopwatch
: System.IDisposable&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; System.Diagnostics.Stopwatch
watch;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; DisposableStopwatch(System.IO.TextWriter
logger)&lt;br&gt;
{&lt;br&gt;
watch &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; System.Diagnostics.Stopwatch.StartNew();&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IDisposable
Members&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Dispose()&lt;br&gt;
{&lt;br&gt;
watch.Stop();&lt;br&gt;
System.Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"{0}"&lt;/span&gt;,
watch.Elapsed);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;
&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Yields something like this.
&lt;/p&gt;
&lt;pre&gt;PInvoke,Native
00:00:00.0003477,00:00:00.0004451
00:00:00.0000705,00:00:00.0003653
00:00:00.0000675,00:00:00.0003550
00:00:00.0000677,00:00:00.0003782
00:00:00.0000679,00:00:00.0003586
00:00:00.0000674,00:00:00.0003772
00:00:00.0000677,00:00:00.0017131
00:00:00.0000706,00:00:00.0004838
00:00:00.0000689,00:00:00.0053451
00:00:00.0000676,00:00:00.0003648
00:00:00.0000677,00:00:00.0003676
00:00:00.0000677,00:00:00.0003659
00:00:00.0000677,00:00:00.0017793
00:00:00.0000766,00:00:00.0004811
00:00:00.0000694,00:00:00.0004966
00:00:00.0000690,00:00:00.0004918
00:00:00.0000704,00:00:00.0003739
00:00:00.0000677,00:00:00.0003609
00:00:00.0000769,00:00:00.0003749
00:00:00.0000677,00:00:00.0034804
PInvoke,Native
00:00:00.0000704,00:00:00.0004326
00:00:00.0000695,00:00:00.0014194
00:00:00.0000770,00:00:00.0002896
00:00:00.0000681,00:00:00.0003052
00:00:00.0000705,00:00:00.0003009
00:00:00.0000711,00:00:00.0003472
00:00:00.0000708,00:00:00.0038629
00:00:00.0000681,00:00:00.0003193
00:00:00.0000678,00:00:00.0003672
00:00:00.0000679,00:00:00.0003150
00:00:00.0000675,00:00:00.0003610
00:00:00.0000679,00:00:00.0003227
00:00:00.0000674,00:00:00.0061228
00:00:00.0000704,00:00:00.0004289
00:00:00.0000701,00:00:00.0004732
00:00:00.0000696,00:00:00.0004107
00:00:00.0000697,00:00:00.0003274
00:00:00.0000678,00:00:00.0003034
00:00:00.0000679,00:00:00.0004169
00:00:00.0000688,00:00:00.0047879
PInvoke,Native
00:00:00.0000703,00:00:00.0004419
00:00:00.0000697,00:00:00.0004428
00:00:00.0000699,00:00:00.0003065
00:00:00.0000678,00:00:00.0003064
00:00:00.0000674,00:00:00.0003073
00:00:00.0000674,00:00:00.0015432
00:00:00.0000694,00:00:00.0004473
00:00:00.0000704,00:00:00.0053116
00:00:00.0000680,00:00:00.0003242
00:00:00.0000698,00:00:00.0003293
00:00:00.0000721,00:00:00.0003163
00:00:00.0000720,00:00:00.0003264
00:00:00.0000716,00:00:00.0039090
00:00:00.0000700,00:00:00.0004544
00:00:00.0000703,00:00:00.0003519
00:00:00.0000678,00:00:00.0003039
00:00:00.0000677,00:00:00.0003300
00:00:00.0000678,00:00:00.0003068
00:00:00.0000677,00:00:00.0070403
00:00:00.0000727,00:00:00.0003814&lt;/pre&gt;
&lt;p&gt;
Looks faster to me. By at least 3x-5x in most cases. That's maybe worth the consideration
to PInvoke it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</comments>
      <category>Code</category>
      <category>CSharp</category>
    </item>
  </channel>
</rss>