post-increment

Why is "while (i++ < n) {}" significantly slower than "while (++i < n) {}"

Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop

final int n = Integer.MAX_VALUE;
int i = 0;
while (++i < n) {
}

is at least 2 orders of magnitude faster (~10 ms vs. ~5000 ms) than:

final int n = Integer.MAX_VALUE;
int i = 0;
while (i++ < n) {
}

I happened to notice this problem while writing a loop to evaluate another irrelevant performance issue. And the difference between ++i < n and i++ < n was huge enough to significantly influence the result.

If we look at the bytecode, the loop body of the faster version is:

iinc
iload
ldc
if_icmplt

And for the slower version:

iload
iinc
ldc
if_icmplt

So for ++i < n, it first increments local variable i by 1 and then push it onto the operand stack while i++ < n does those 2 steps in reverse order. But that doesn't seem to explain why the former is much faster. Is there any temp copy involved in the latter case? Or is it something beyond the bytecode (VM implementation, hardware, etc.) that should be responsible for the performance difference?

I've read some other discussion regarding ++i and i++ (not exhaustively though), but didn't find any answer that is Java-specific and directly related to the case where ++i or i++ is involved in a value comparison.

Is there a performance difference between i++ and ++i in C++?

We have the question is there a performance difference between i++ and ++i in C?

What's the answer for C++?

Pre increment vs Post increment in array

I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book.

main( ) 
{ 
 int a[5] = { 5, 1, 15, 20, 25 } ; 
 int i, j, k = 1, m ; 
 i = ++a[1] ; 
 j = a[1]++ ; 
 m = a[i++] ; 
 printf ( "\n%d %d %d", i, j, m ) ; 
}

My understanding was, it will print i as 2, j as 1 and m as 15

But somehow it is printing as i as 3, j as 2 and m as 15? Why is it so?

Below is my understanding-

b = x++;
In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used.

b = ++y;
In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6 because the value of ‘y’ gets modified before using it in a expression.

Is there anything wrong in my understanding?

Post-increment within a self-assignment

I understand the differences between i++ and ++i, but I'm not quite sure why I'm getting the results below:

static void Main(string[] args)
{
    int c = 42;
    c = c++;
    Console.WriteLine(c);   //Output: 42
}

In the above code, as this is assigning the variable to itself and then incrementing the value, I would expect the result to be 43. However, it is returning 42. I get the same result when using c = c--; as well.

I realise I could just simply use c++; and be done with it, but I'm more curious why it is behaving the way that it is. Can anyone explain what's happening here?

Incrementing in C++ - When to use x++ or ++x?

I'm currently learning C++ and I've learned about the incrementation a while ago. I know that you can use "++x" to make the incrementation before and "x++" to do it after.

Still, I really don't know when to use either of the two... I've never really used "++x" and things always worked fine so far - so, when should I use it?

Example: In a for loop, when is it preferable to use "++x"?

Also, could someone explain exactly how the different incrementations (or decrementations) work? I would really appreciate it.

Is there a performance difference between i++ and ++i in C?

Is there a performance difference between i++ and ++i if the resulting value is not used?

What is the difference between pre-increment and post-increment in the cycle (for/while)?

My interest is in the difference between for and while loops. I know that the post-increment value is used and then incremented and the operation returns a constant pre-increment.

while (true) {
    //...
    i++;
    int j = i;
}

Here, will j contain the old i or the post-incremented i at the end of the loop?

increment value of int being pointed to by pointer

I have an int pointer (i.e., int *count) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call:

*count++;

However, I am getting a build warning "expression result unused". I can: call

*count += 1;

But, I would like to know how to use the ++ operator as well. Any ideas?

The difference between ++Var and Var++

In programming, particularly in Java, what is the difference between:

int var = 0;
var++;

and

int var = 0;
++var;

What repercussions would this have on a for loop?

e.g.

for (int i = 0; i < 10; i++) {}

for (int i = 0; i < 10; ++i) {}
Pre- & Post Increment in C#

I am a little confused about how the C# compiler handles pre- and post increments and decrements.

When I code the following:

int x = 4;
x = x++ + ++x;

x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5, which makes it 5+5 which evaluates to 10. Then the post-increment will update x to 6, but this value will not be used because then 10 will be assigned to x.

But when I code:

int x = 4;
x = x-- - --x;

then x will be 2 afterwards. Can anyone explain why this is the case?

Difference between pre-increment and post-increment in a loop?

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

String concatenation while incrementing

This is my code:

$a = 5;
$b = &$a;
echo ++$a.$b++;

Shouldn't it print 66?

Why does it print 76?

What is x after "x = x++"?

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!

Is the behavior of return x++; defined?

If I have for example a class with instance method and variables

class Foo
{

   ...

   int x;
   int bar() { return x++; }
 };

Is the behavior of returning a post-incremented variable defined?

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?

Post-increment and Pre-increment concept?

I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?

Java increment and assignment operator

I am confused about the post ++ and pre ++ operator , for example in the following code

int x = 10;
x = x++;

sysout(x);

will print 10 ?

It prints 10,but I expected it should print 11

but when I do

x = ++x; instead of x = x++;

it will print eleven as I expected , so why does x = x++; doesn't change the the value of x ?

a = (a++) * (a++) gives strange results in Java

I'm studying for the OCPJP exam, and so I have to understand every little strange detail of Java. This includes the order in which the pre- and post-increment operators apply to variables. The following code is giving me strange results:

int a = 3;

a = (a++) * (a++);

System.out.println(a); // 12

Shouldn't the answer be 11? Or maybe 13? But not 12!

FOLLOW UP:

What is the result of the following code?

int a = 3;

a += (a++) * (a++);

System.out.println(a);
Preincrement faster than postincrement in C++ - true? If yes, why is it?

I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?

Java: Prefix/postfix of increment/decrement operators

From the program below or here, why does the last call to System.out.println(i) print the value 7?

class PrePostDemo {
    public static void main(String[] args) {
        int i = 3;
        i++;

        System.out.println(i);    // "4"

        ++i;
        System.out.println(i);    // "5"
        System.out.println(++i);  // "6"
        System.out.println(i++);  // "6"
        System.out.println(i);    // "7"
    }
}
++i or i++ in for loops ??

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

Is there a reason some programmers write ++i in a normal for loop instead of writing i++?

Difference between *ptr += 1 and *ptr++ in C

I just started to study C, and when doing one example about passing pointer to pointer as a function's parameter, I found a problem.

This is my sample code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int* allocateIntArray(int* ptr, int size){
    if (ptr != NULL){
        for (int i = 0; i < size; i++){
            ptr[i] = i;
        }
    }
    return ptr;
}

void increasePointer(int** ptr){
    if (ptr != NULL){
        *ptr += 1; /* <----------------------------- This is line 16 */
    }
}

int main()
{
    int* p1 = (int*)malloc(sizeof(int)* 10);
    allocateIntArray(p1, 10);

    for (int i = 0; i < 10; i++){
        printf("%d\n", p1[i]);
    }

    increasePointer(&p1);
    printf("%d\n", *p1);
    p1--;
    free(p1);
    fgets(string, sizeof(string), stdin);
    return 0;
}

The problem occurs in line 16, when I modify *ptr+=1 to *ptr++. The expected result should be the whole array and number 1 but when I use *ptr++ the result is 0.

Is there any diffirence between +=1 and ++? I thought that both of them are the same.

Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

Why does this

 int x = 2;
    for (int y =2; y>0;y--){
        System.out.println(x + " "+ y + " ");
        x++;
    }

prints the same as this?

 int x = 2;
        for (int y =2; y>0;--y){
            System.out.println(x + " "+ y + " ");
            x++;
        }

As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop?

Strange Increment Behaviour in C#

Note: Please note that the code below is essentially non-sense, and just for illustration purposes.

Based on the fact that the right-hand side of an assignment must always be evaluated before it's value is assigned to the left-hand side variable, and that increment operations such as ++ and -- are always performed right after evaluation, I would not expect the following code to work:

string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];

int IndTmp = 0;

foreach (string TmpString in newArray1)
{
    newArray2[IndTmp] = newArray1[IndTmp++];
}

Rather, I would expect newArray1[0] to be assigned to newArray2[1], newArray1[1] to newArray[2] and so on up to the point of throwing a System.IndexOutOfBoundsException . Instead, and to my great surprise, the version that throws the exception is

string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];

int IndTmp = 0;

foreach (string TmpString in newArray1)
{
    newArray2[IndTmp++] = newArray1[IndTmp];
}

Since, in my understanding, the compiler first evaluates the RHS, assigns it to the LHS and only then increments this is to me an unexpected behaviour. Or is it really expected and I am clearly missing something?

Incrementor logic

I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression :

public static void main(String[] args) {
    int i = 0;
    i = i+=(++i + (i+=2 + --i) - ++i);
    // i = 0 + (++i + (i+=2 + --i) - ++i);
    // i = 0 + (1   + (3    +   2) -  1 );
    // i = 0 + (6                  -  1 );
    System.out.println(i); // Prints 0 instead of 5
}

I know I'm missing the logic somewhere but where?

What I've tried :

  • Going from left to right (though I know it is not recommended)
  • Going from the insidest bracket and starting from there.

Thanks for the help

PS : The comments are the details of my calculus

EDIT 1

I tried to change de hard coded value from the expression from 2 to something else and the result always gives 0

Look at this example :

    int i = 0;
    i = i+=(++i + (i+=32500 + --i) - ++i);
    System.out.println(i); // Prints 0

This expression should logically be nowhere near 0 but somehow it does print it.

The same happens when I use a negative :

    int i = 0;
    i = i+=(++i + (i+=(-32650) + --i) - ++i);
    System.out.println(i); // Prints 0

EDIT 2

Now, I changed the value of i to begin with :

    int i = 1;
    i = i+=(++i + (i+=2 + --i) - ++i);
    System.out.println(i); // Prints 2
    
    i = 2;
    i = i+=(++i + (i+=10000 + --i) - ++i);
    System.out.println(i); // Prints 4
    
    i = 3;
    i = i+=(++i + (i+=(-32650) + --i) - ++i);
    System.out.println(i); // Prints 6

It gives the double of i each time, whatever the hard coded value is.

Post increment operator not incrementing in for loop

I'm doing some research about Java and find this very confusing:

for (int i = 0; i < 10; i = i++) {
  System.err.print("hoo... ");
}

This is never ending loop!

Anybody has good explanation why such thing happens?

Is ++x more efficient than x++ in Java?

During a programming class, the professor was teaching us about x++ and ++x, with x being an integer.

He said that in the scenario we are able to just put either x++ or ++x, ++x is more efficient (by little, but still, in theory, more efficient nonetheless).

But I forgot why. Anyone knows? This was with Java.

Why doesn't the post increment operator work on a method that returns an int?
public void increment(){
    int zero = 0;

    int oneA = zero++; // Compiles

    int oneB = 0++; // Doesn't compile

    int oneC = getInt()++; // Doesn't compile
}

private int getInt(){
    return 0;
}

They are all int's, why won't B & C compile? Is it to do with the way ++ operator differs from = 0 + 1;?

Invalid argument to operation ++/--

How do the post increment (i++) and pre increment (++i) operators work in Java?

Can you explain to me the output of this Java code?

int a=5,i;

i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;

System.out.println(a);
System.out.println(i);

The output is 20 in both cases

Why can't I do ++i++ in C-like languages?

Half jokingly half serious : Why can't I do ++i++ in C-like languages, specifically in C#?

I'd expect it to increment the value, use that in my expression, then increment again.

post-increment