Jump to content
  • Advertisement
Sign in to follow this  
AndyTang

[.net] c# and increment operator

This topic is 3578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just needed confirmation on something about c#. I have always been told that in C++, the pre-increment (++n) operator was the fastest operation for incrementing a integer. However, I've now been informed that this isn't the case anymore with c#. n = n + 1 is just as fast as n++ and ++n. Is this true?

Share this post


Link to post
Share on other sites
Advertisement
I personally wouldn't worry about such trivial things. Worry more about the structure of your app and the big things you are doing. Once you get everything up and running, profile it to see where the slow bits are, and then work on those.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrominium
Just needed confirmation on something about c#.

I have always been told that in C++, the pre-increment (++n) operator was the fastest operation for incrementing a integer. However, I've now been informed that this isn't the case anymore with c#. n = n + 1 is just as fast as n++ and ++n. Is this true?


If you're THAT worried about performance I wouldn't be using C#. (C# programs aren't that much slower than C++ programs but there is a trivial gain.)

Share this post


Link to post
Share on other sites
Quote:
Original post by cyansoft
False according to this.


Thanks for the article. :) But the article states there are no performance difference between post- and pre-increment except for static variables.

I'm just curious about performance at the moment because I'm working on some AI algorithms. I've recently ported some code from C++ to C# but I found it much much slower (about 3x slower). But this is only because I know how to optimise my C++ code (ie use of pointer arithmetic and for array iterations). Anyone know much about optimising code in C#?

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrominium
Anyone know much about optimising code in C#?


A little. Most of it comes to making sure your heavy processing bits don't allocate new objects (and thus require GC time). But first things first, what does your profiler say is a problem area?

Share this post


Link to post
Share on other sites
mmh, I think it all comes to my evaluation method.

I'm trying to find how many items are unique in an array. I do this by creating a boolean array, all set to false. As it reads in each number, it flips the boolean value to true. At the end, I count the number of trues.

On another note, strangely enough, I had a small performance increase when I changed my storage data from bytes to integers.

Share this post


Link to post
Share on other sites
Quote:
Original post by mpipe
Quote:
Original post by Chrominium
Just needed confirmation on something about c#.

I have always been told that in C++, the pre-increment (++n) operator was the fastest operation for incrementing a integer. However, I've now been informed that this isn't the case anymore with c#. n = n + 1 is just as fast as n++ and ++n. Is this true?


If you're THAT worried about performance I wouldn't be using C#. (C# programs aren't that much slower than C++ programs but there is a trivial gain.)


This was true of .Net 1.1 and earlier. (I am the person that performed the benchmark comparisons between Native and Managed code when the first version of VS.Net was in development)

Nowadays, most operations on Windows for managed and native code are exactly equivalent. I know this is surprising in some ways. I think that the reason is because the same system calls in most cases are being called. This would make the difference between a native call and a managed call differ by maybe a millisecond, but my benchmark did not even show that much difference. So, there is likely some other compiler or jit trickery going on back there.

Anyway, just thought that was interesting.

Still though just to be fair, C++ is easier to use when you want to do fast memory manipulation. C# will make you jump through some hurdles to accomplish some similar things. Differences in ease of implementation can make a more than trivial difference imho.

Take care.
Brad

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!