this:
Code: Select all
while(r=p>2&r<0?-r:-o[++j]) {...}
Code: Select all
while(r=p>2&r<0?-r:-o[j++]) {...}
Code: Select all
while(r=p>2&r<0?-r:-o[++j]) {...}
Code: Select all
while(r=p>2&r<0?-r:-o[j++]) {...}
They do, since the value of the increment operation is not used (only the side effect).doc-helmut wrote:I'm not sure if the both following do the same (in C, not in C++):(not at home, can't test it)Code: Select all
for(int i = 0; i < 42; i++) {printf("%d\n", i} // vs. for(int i = 0; i < 42; ++i) {printf("%d\n", i}
And the (++i) version can be faster in this case than the (i++) version. At least in C++ when i is an object (but I can't be bothered to lookup the quote in Stroustrup / Meyers now).bullestock wrote:They do, since the value of the increment operation is not used (only the side effect).doc-helmut wrote:I'm not sure if the both following do the same (in C, not in C++):(not at home, can't test it)Code: Select all
for(int i = 0; i < 42; i++) {printf("%d\n", i} // vs. for(int i = 0; i < 42; ++i) {printf("%d\n", i}
You got that the wrong way round....doc-helmut wrote:the prefix ++i version usually uses temporary variables to store the old index in case the old value (i) is needed while i already has been incremented to (i+1).
Code: Select all
int increment_before(int& i) // ++i
{
i = i+1;
return i;
}
// and
int increment_after(int& i) // i++
{
int temp= i; // remember old value
i = i+1;
return temp;
}
Code: Select all
std::vector<int> integerVector;
// Fill vec with values
std::vector<int>::iterator i = integerVector. begin();
for ( ; i != integerVector. end(); ++i)
{
// Do something with current value
cout << (*i) << endl;
}
Code: Select all
MyClass increment_before(MyClass& k) // ++k
{
// increment k here
return k;
}
// and
MyClass increment_after(MyClass& k) // k++
{
MyClass temp = MyClass(k); // remember old value
// increase k here
return temp;
}
Yes, but if there is a rule which doesn't matter in the worst case but helps in the best case and applies for C and C++, you might as well remember it.On the other hand, we are talking about C and NXC, not C++
The standard only specifies how the language has to work. Whether the compiler optimizes away the temp-variable or not is a choice of implementation. So it indeed depends on the compiler and optimization-level!I doubt that ANSI C prefix ++i is faster than postfix i++
Wrong, as we've seen above. The temp-variable is indeed needed with i++ and not with ++i. Whether the compiler makes the actual increment-step a register operation, and if it optimizes away the temp-variable when it's not needed, is compiler-specific.which is simply the fastest i=i+1 without any temp variable not even using the RAM for calculation (in C it's a direct register operation).
You may decide that for yourself, but 1) now you know your understanding of ++i vs i++ was wrong, and 2) now everybody can safely trust the "in general, ++i is equal or faster than i++ for incrementations only, always" rule.So please CMIIW, but honestly, I'm not sure if your post helps a lot along, Linus
Code: Select all
std::vector<int> integerVector; // Vektor mit Werten füllen std::vector<int>::iterator i = integerVector.begin(); for ( ; i != integerVector.end(); ++i) { // Mache irgendwas mit dem aktuellen Element, z.B. cout << (*i) << endl; }
Code: Select all
MyClass increment_before(MyClass& k) // ++k { //Erhöhen von k return k; } // und MyClass increment_after(MyClass& k) // k++ { MyClass temp = MyClass(k); // Den alten Wert sichern //Erhöhen von k return temp; }
Users browsing this forum: Semrush [Bot] and 1 guest