简体   繁体   中英

What is recommended ++ or +1 in C++?

S++ or S=S+1 , which can be recommended to increment the value by 1 and why?

I think S++ should be preferred, as it is single machine instruction (INC) internally. Make me correct if I'm wrong. Other way I think both are same except ++ is unary and its post-increment and operator overloading is different for both.

Does is make any difference in C#?

You should never try to do such tiny optimizations. The modern compilers are smart enough to generate identical assembler code for all 3 options ( ++i , i+=1 , i=i+1 ) for almost all cases.

Yes, sometimes you can gain some tiny performance boost, for example, using prefix ++ instead of postfix for some class with very complicated ++ operators, but in vast majority of cases part of your program which takes most time is not increments, so to achieve some real results you should profile your code on real (or close to real) test and find less effective frequently used pieces of code.

Also, there's a more important thing to consider - it's code readability. In vast majority of situations it's more important than performance, and only in the most time-critical pieces of code we can sacrifice readability for performance (and even there, not always! sometimes it's better just to add more hardware).

So, my recommendation would be use the option which states your idea in the most clear way .

Back in the good old days, this sort of thing made a difference. I used to look at the generated machine code, then tweak the C source to get an extra 10% speed improvement.

Not any more. You cannot predict the speed from looking at machine code.

  • Many of the optimizations these days are done in the CPU.
  • The IL code is optimized again at runtime, for the processor it is running on, so as a developer you never get to see the instructions which are executed.
  • CPUs are so fast that it hardly matters.

You could try testing the different ways of incrementing, see which one is faster. Once you've got bored with that, stop worrying about speed and concentrate on writing clear, bug-free code.

C++ has both pre and post-increment operators .
Pre-increment operator is usually faster as already answered here .
(mostly because compilers can optimize it better, and it does not require a temporary copy of the old value to be returned).

Between incrementing a variable s by doing s = s + 1 OR s++, we first need to accept that the operation in itself is really basic, simple and straightforward. We are basically incrementing a value stored in a variable by a fixed number. Even though s++ will be quicker to execute than s = s + 1, the time difference between these 2 different methods will be the same. I tried the following code:

    int s = 1;
    s++;

And it gave the following compilation and execution time:

Compilation time: 0.32 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0,46 sec

Following that, I tried the code below:

    int s = 1;
    s = s + 1;

And it gave the following execution time:

Compilation time: 0.32 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0,46 sec

So, just as I said, there's no significant difference between the 2 operations as they in themselves are quite simple. A time difference does exist; however, the execution time given above was not so specific, meaning that there must be a time difference that is >0.001, Maaking it pretty much negligible. Hope this answers your question.

++ is more powerful. you can do things like that

if(a++ > 7) b=0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM