c# performance  
Author Message
ycseattle





PostPosted: Common Language Runtime, c# performance Top

I am working on performance tuning for an app. The old code is the following:

for(int i=0; i<10000; i++)
{
    MyClass a = MyClassCollection[ i ];
}

If I declare the reference type a outside of the loop like the following, will I get better performance

MyClass a=null;
for(int i=0; i<1000; i++)
{
    a = MyClassCollection[ i ];
}

It seems to me that there is a performance gain since I am not allocating new memory for reference type a during each loop iteration, but I want to confirm this.



.NET Development27  
 
 
George Clingerman





PostPosted: Common Language Runtime, c# performance Top

In short. Yes.

If you want to read the religiousreligous wars on this topic just do searches on the forums or the web. This topic has been covered extensively in a variety of places already.

If you're a beginning game developer are you going to be able to notice the differences No.




 
 
duck thing





PostPosted: Common Language Runtime, c# performance Top

Intuitively that seems right -- though I doubt you'd see a very big performance gain by making the change. What happens if you just try it



 
 
IBRAHIM ERSOY





PostPosted: Common Language Runtime, c# performance Top

of course no! I have begun C# 2-3 months ago.and learned much thing so far.

i bought Tom Miller's Beginning 3D Game Programming.And there he was telling a little difference.Like about %7 less performance.But faster development than C++.

and thats good thing.



 
 
Vipul Patel - MSFT





PostPosted: Common Language Runtime, c# performance Top

There should not be any noticable difference in the output. I am assuming that you meant to iterate over 10000 (your second snippet just mentions 1000) times.

Lets notice what the cost of first declaration is:

|

| -- current ESP

|

In the loop, the ESP will move by 4 bytes for the address of A when it executes the following line

MyClass a = MyClassCollection[ i ];

And these 4 bytes will point to an object in the MyClassClollection(). At the end of the loop, the ESP will move back by 4 bytes (assuming 32 bit OS)

In the second case, the movement will not occur and "a" will directly point the corresponding object in the MyClassCollection. The ESP movement is already done before we reach the loop.

When you have the first case and a smart compiler liek C#/CL/VB, internally it is the case (b) which will actually happen.



 
 
Greg Beech





PostPosted: Common Language Runtime, c# performance Top

Um, actually the generated IL will be semantically identical. All locals are declared in the method header irrespective of where you declare them in the method body. So there will be no performance difference whatsoever.

Try it both ways and look at the output using ILDASM if you don't believe me...



 
 
George Clingerman





PostPosted: Common Language Runtime, c# performance Top

The percentages faster for performance and development that are tossed around are fairly arbitrary and really hard to come up with. And they are going to change depending on the developer and the kind of development you are talking about. Interesting to discuss, if you like discussing that kind of thing, but eventually leads you nowhere. Kind of like a philosophy discussion.

If you're happy with C# and are interested in getting into game development, then don't worry about performance yet. That's a back of the mind thought for now. Too many new game developers get hung up on this issue to the point that they never go anywhere or get anything done.

Make a game, make a few games and once you understand the concepts behind them, then start looking at performance. It should always be in the back of your mind, but never take up so much of your thoughts that you make no progress.

C# is a great place to start and if you're really beginnnig, I'd recommend looking at C# game development with XNA. Very nice to get into and there's a huge growing community right now.


 
 
Jim Perry





PostPosted: Common Language Runtime, c# performance Top

Everytime this issue comes up I go back to what the Garage Game guys said when they ported Marble Blast Ultra over to C# and XNA - no noticeable performance degradation. End of story. Since it's very unlikely indie developers will be doing something that'll come close to taxing the 360's hardware or a mid-level PC, don't worry about it.

Geez!



 
 
Greg Beech





PostPosted: Common Language Runtime, c# performance Top

Also, this type of thing isn't your bottleneck. Honestly. Saving a few clock cycles here and there won't do anything for your overall performance, because you aren't fixing the bottleneck.

If you need to tune the performance, then get a code profiler on it (such as the one in VS2005) and measure where your bottleneck is, then fix that. Then measure again. Then fix than. Until you reach your performance goals. Then stop, as you're just wasting money making a system faster than it needs to be.

Or, if it's an option, just buy more hardware. That's often cheaper than performance tuning.



 
 
IBRAHIM ERSOY





PostPosted: Common Language Runtime, c# performance Top

thank u so much i will use C#.thanks a lot



 
 
Inaki Ayucar





PostPosted: Common Language Runtime, c# performance Top

After using APIs like Glide, and later OpenGL for some years, I moved to DirectX C++ (since version 7), and I’ve been programming C++ DirectX since that.

In the last 4 years, we’ve been experimenting with C# and actually, we’ve developed a .Net C# only full driving simulator, with full physics, interaction, 3d Graphics, and a 3D modeling and level editor application.

http://www.youtube.com/watch v=CUFIW1Wzkko

http://www.simax.es

What I can tell you is: yes, there’s a small performance difference, but it can be something perfectly affordable sometimes, as development times and risks get reduced a lot.

And I mean A LOT !



 
 
thedo





PostPosted: Common Language Runtime, c# performance Top

Another interesting performance statistic of C# (and the .net framework in general) is that ultimately the speed depends on the runtime implementation of the .net framework. as C# code is compiled down to IL level and then JIT'ed at runtime the runtime is what gives the performance. Say in a years time MS update the .net CF2 framework for 360, to utilise a load of features of the CPU which are currently unimplemented, without recompilation your app could gain in speed by magnitudes. However once a C++ app is compiled, unless it depends on some other runtime library which is updated its performance is likely to be pretty static. This not only makes generalisation of speed comparisons difficult, but also quite dangerous as its not a static performance variable. Obviosuly wherever the .net framework is reliant on what is essentially a C++ component (ie DirectX in XNA), you can expect the .net framework to be slower purely through any overhead of the additional call of managed code to unmanaged code, but other variables are far from static.

N



 
 
magicalclick





PostPosted: Common Language Runtime, c# performance Top

C# is 100% slower because it is not directly using CPU. But from what I have heard, the performance is really close. MS official said they aimed to have performance with-in 7% slower compare to C++. Asides from the exact performance, C# lets you to make cleaner code, safer code, easier code. This gives you more time to focus on optimization on higher level instead of stuck at lower level. Also most games are GPU intensive, not so much CPU intensive. You should worry about your DX engine more than C#.