views:

515

answers:

8

Platform: .NET Framework 3.5 SP1 on x32

Are there any performance issues regarding leaving an empty statement (";", by itself) in code?

And to be marked as an answer would you also teach a man (me? and other people reading this) to fish? Meaning, how to figure out there is a performance issue with it?

A: 

I can't imagine any case in which a compiler wouldn't simply optimize an empty statement away. It certainly wouldn't generate instructions that mean anything other than "do nothing". If you think there's a performance issue with this, chances are good that it's something else.

John Feminella
+1  A: 

The easiest way to see if there would be a performance issue (since I don't think there would be) is to run the debugger and see if that line is executed. if it is executed, try to get to the assembly behind that statement, as that would give you an indication on how many instructions result from that statement. Chances are though, it is a no-op, which is just a clock tick.

MasterMax1313
"it is a no-op, which is just a clock tick." That is what I am trying to find out how to figure it out ;)
Sung Meister
+14  A: 

1) Use a tool like ildasm or .NET Reflector to look inside the assembly that is generated and see what IL instructions are associated with the ";" empty statement (if any at all; they might get optimized away into nothing.)

2) Use a profiler to run a huge loop with a bunch of ";"s included inside other code, and then try it without the ";"s and see if there's a difference.

(Without doing either of these I'd bet quite a lot that it's optimized away and produces no IL (or some kind of no-op instruction -- pardon my IL ignorance.))

mquander
+1 for the ILDASM/Reflector answer, which is just what I would have suggested.
Daniel Earwicker
I have never messed around with ildasm before because i never knew the purpose of it.
Sung Meister
It gets really powerful when you add it as an external tool to Visual Studio, then you can just build it and run the tool and see the IL. http://www.devx.com/vb2themax/Tip/18784
Samuel
+1 for the external tool to VS tip from Samuel!
Svish
+6  A: 

The empty statement yields the nop IL code, so you can have as many as you like as they are removed by the JIT compiler.

Brian Rasmussen
+1 for "nop"
Sung Meister
IIF not running in debug mode :)
leppie
A: 

I do not expect a performance hit.

You can check this by inspecting the generated IL code for a C# program with and without empty statements. You may use .NET reflector (www.redgate.com) to inspect the IL. If the same IL is generated, there cannot be a performance hit.

Renze de Waal
+2  A: 

The simplest way to measure performance is to write your code and run it multiple times whilst timing it.

As for this scenario, I do not believe you will lose any code performance for having empty lines, however, it may affect your compile times, (albeit negligable).

Robin Day
+1 for being right, and for your cool picture.
Mike Dunlavey
A: 

In the spirit of teaching you to fish,

1) examine the assembly, as was suggested

2) run a big loop & time it (a wristwatch is a perfectly good tool)

3) single-step it in the disassembly window (similar to 1).

If you are concerned about performance, the wrong thing to do is to start guessing about language constructs. The right thing to do is to diagnose, and let the program tell you what is taking time, as in

4) my favorite method

Mike Dunlavey
+1  A: 

As an alternate way to test performance:

Start out with:

// breakpoint here
int breakpoint1 = 0;

for (int i = 0; i <= 1000; i++)
{
   // test operation here
   ;
}

// breakpoint here
int breakpoint2 = 0;

Run to the first break point, then press F5 and count in your head. If it goes too fast then change the loop to 10,000 and try again. If it's too fast increase it to 100,000 and try again.

It would probably be more precise if you recorded the times before and after the for loop rather than counting in your head.
DyreSchlock
The point of it is to run a quick test without the need for any instrumentation. If you can't tell the difference between the two tests you need to add another zero until you can. If that doesn't work (i.e. it runs too long) you need to pull out a real profiler.
yes, but it would be faster to declare 2 variables, store the start and end time, subtract start time from end time and printing it than going through trial and error to fin the right amount of loops and counting in your head is far from precise.
Martin