I want to get a Process ID for the current Application in C#, so which is faster (and does it really matter).
Lets find out!
namespace ProcessIDFaceoff
{
class Program
{
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
static extern int GetCurrentProcessId();
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
new Program().RunTest();
}
}
public void RunTest()
{
const int times = 1000;
System.Console.WriteLine("PInvoke,Native");
for (int j = 0; j < 20; j++)
{
using (new DisposableStopwatch(System.Console.Out))
{
for (int i = 0; i < times; i++)
{
int pid = GetCurrentProcessId();
}
}
System.Console.Write(",");
using (new DisposableStopwatch(System.Console.Out))
{
for (int i = 0; i < times; i++)
{
int pid = System.Diagnostics.Process.GetCurrentProcess().Id;
}
}
System.Console.WriteLine();
}
}
class DisposableStopwatch : System.IDisposable
{
private System.Diagnostics.Stopwatch watch;
public DisposableStopwatch(System.IO.TextWriter logger)
{
watch = System.Diagnostics.Stopwatch.StartNew();
}
#region IDisposable Members
public void Dispose()
{
watch.Stop();
System.Console.Write("{0}", watch.Elapsed);
}
#endregion
}
}
}
Yields something like this.
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
Looks faster to me. By at least 3x-5x in most cases. That's maybe worth the consideration to PInvoke it.