Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Even with -O0 they're identical on GCC and Clang so it doesn't really matter
https://godbolt.org/z/xL-hcx
https://godbolt.org/z/H7Cb-v
I guess preincrement is the more sensible of the two so why not go with that one -
@yellow-dog Because you're allocating an iterator/stream object -- likely on the heap for managed memory languages -- then you have dynamic dispatch overhead of calling the next method or if passing a lambda you again have the overhead of an object allocation and dynamic dispatch
In most languages a simple for loop will be faster. But of course it all depends on the implementation and I couldn't quite figure out what language @Demolishun was talking about since he's a C++ guy but the example looks like Java or JavaScript -
++i or i++ doesn't matter if i is a plain old data type, which is always the case in C. In C++ however, it might be some object type with overloaded addition operator, and then ++i may be faster.
-
@12bitfloat it would turn out, you tried to be smarter than the compiler
@highlight
edit: i love how my terminal doesnt copy formatting
Related Rants
To preincrement or to postincrement?
for(var i = 0; i<length; ++i){
}
or
for(var i = 0; i<length; i++){
}
I would imagine on a good vm or compiler this would be optimized anyway. So does it really matter?
question
increment
loop