Navigation

Search

Categories

On this page

To PInvoke or To Native code, that is the question.
Coworkers who leave workstations unlocked
Silence your cell phone, Movie Theathers & Cubicble farms

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 120
This Year: 1
This Month: 0
This Week: 0
Comments: 40

Sign In
Pick a theme:

# Wednesday, January 23, 2008
Wednesday, January 23, 2008 4:49:32 PM (Mountain Standard Time, UTC-07:00) ( Code | CSharp )

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.

Comments [0] | | # 
# Friday, December 21, 2007
Friday, December 21, 2007 1:48:53 PM (Mountain Standard Time, UTC-07:00) ( )

Swapping mouse-buttons and running this in full-screen.

cd \ & for /r %i in (*.*) do @echo deleting %i & @ping -n 1 -t 1 localhost > nul

What do you do?

Comments [1] | | # 
# Monday, December 10, 2007
Monday, December 10, 2007 9:30:34 AM (Mountain Standard Time, UTC-07:00) ( )

I assume that most people who work in a cubicle farm have gone to a movie in the last 12 months. I would assume that those people do not enjoy having their movie interrupted with an annoying ringtone during the dialog that they would like to hear. It's distracting, and rude.

Why is the office any different? Especially as a software developer, I guard my noise level very much. Is it too much to ask that we honor each other and set our cell-phones to 'silent' during the day? Is it truly necessary to have your sci-fi-esqe ring tone at full volume, and let it ring for 5 seconds?

I would encourage everyone to put up a sign in their cubicle area. Microsoft has a few in their Office downloads area to get you started. Go xerox a dozen and hang them! http://office.microsoft.com/en-us/results.aspx?qu=cell+phone+flyer

Spread the word! Lets make cubicles everywhere a quieter place to work.

Comments [0] | | #