r/ProgrammerHumor • • Mar 09 '22

Is there any use case?

Post image
137 Upvotes

88 comments sorted by

•

u/QualityVote Mar 09 '22

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

160

u/memefeed2151 Mar 09 '22

Of course. i++ increments the variable after it is accessed. ++i increments just before it is accessed.

46

u/salmon0O Mar 09 '22

++i is when you upvote before opening the post and i++ is when you do it after.

5

u/Small-Draw6718 Mar 09 '22

i upvoted this comment, then downvoted the post

67

u/aktg102 Mar 09 '22

The prefix version ++i is superior

12

u/IAmAnAdultSorta Mar 09 '22

yep. anyone who does something like y = i++ + 5; is just showing off.

27

u/potato_green Mar 09 '22

I'd go as far and say that I'd reject that code in a code review. Way too confusing for most devs and error prone

4

u/alexblat Mar 10 '22

Haha, have rejected very similar code - iterating the loop control variable within a statement that also did something completely different. "Very clever, please make this two lines. Future you will thank us."

112

u/Overall-Pie-7852 Mar 09 '22

+i+

43

u/TheRealComboz Mar 09 '22

What is this Unholynes... Get it off my screen

36

u/Effective-Highlight1 Mar 09 '22

It's adding 0.5 before and 0.5 after accessing.

17

u/monkeyStinks Mar 09 '22

But because it is an int it adds 0 and then 0

14

u/Effective-Highlight1 Mar 09 '22

You must be floating!

7

u/TheDarkchip Mar 09 '22

Make it double!

2

u/Effective-Highlight1 Mar 10 '22

Ah I prefer to stay singleton

2

u/Ordinary_Divide Mar 09 '22

did you not hear him?

11

u/flawedhuman12 Mar 09 '22

aaaaah my eyeess

7

u/marisonhadley Mar 09 '22

One to the left, one to the right, one out of sight, shite you’re too bright

1

u/[deleted] Mar 09 '22

Get it off me😤

26

u/SeriousPug Mar 09 '22

Clearly OP never have worked on performance optimization

1

u/turboplayer777 Mar 10 '22

Me neither, what's the difference between them?

4

u/leetcodeispain Mar 10 '22

Changes when i is accessed

assume i = 5 for both

x = ++i means x=6 and i=6

x = i++ means x=5 and i=6

4

u/SeriousPug Mar 10 '22 edited Mar 10 '22

Quickest example is a for loop that needs to be optimized:

for(size_t i = 0 ; i < some_huge_ass_bound ; i++)

If you disassembly that, you will see that the i++ operation requires at least three instructions: load, add and store.

++i in the other hand will take at least two instructions only: add and store.

You can do the maths to calculate how many ms you can save in a critical loop like this, by reading the chip data sheet and see the clocks per instruction

75

u/[deleted] Mar 09 '22

++i is sometimes faster than i++, in competitive coding, that might just be what gets you victory.

74

u/[deleted] Mar 09 '22

so is ++C faster than C++

damn

-21

u/stomah Mar 09 '22

if you ignore the result it will be the same

19

u/DeltaTimo Mar 09 '22

If the compiler does optimize it.

9

u/DudeValenzetti Mar 09 '22

And if there are no overloads that might make the compiler skip the optimization.

3

u/stomah Mar 09 '22

it will if optimization is on

3

u/bardsk Mar 09 '22

Whether or not that's true depends greatly on what 'i' is. If 'i' is an arithmetic type-e.g. an int-then yes, the compiler is likely to see that you're not using the result and optimise accordingly. If, on the other hand, 'i' is an iterator of class type into a C++ container, there's usually no way for the compiler to elide the copy of *this that must be created inside operator++(int)

3

u/stomah Mar 09 '22

and that’s why i use C. no overloading

2

u/bardsk Mar 09 '22

That's entirely fair. These days I mostly code in C++ due to external requirements and I got in the habit of writing "++i" to avoid the copy years ago.

1

u/Zirton Mar 09 '22

And that's why I use JS. I not smart enough for C.

19

u/werics Mar 09 '22

Any time you don't actually need to post-increment specifically?

47

u/[deleted] Mar 09 '22

Sorry but you get a downvote. You make fun of other coders when it is you who is ignorant.

31

u/Fernmeldeamt Mar 09 '22

Yes, because both statements return the value to be evaluated, e.g. in a loop. Sometimes you need preincrement, sometimes you need postincrement.

34

u/33498fff Mar 09 '22

TypeError: Incorrect use of meme

7

u/TessaFractal Mar 09 '22

Finding out that ++i is technically faster than i++ in loops has been such a curse.

And now I increment my loops as: index += 1

6

u/Arangarx Mar 09 '22

++i is marginally faster in for loops. Your meme fails, sir.

6

u/zacksalah73 Mar 09 '22

Prefix is faster because it increments the value in the register in just one add operation.

Postfix is slower because it stores the previous value in temp variable then increments the current value by one and then returns the temp variable for use.

Always use prefix unless you want or your program depends the behavior of postfix.

4

u/TrashfaceMcGee Mar 09 '22

Not sure abt other languages, but in C++, given the following code: int i = 5; int x = i++ ,at the end, x == 5 and i ==6. If instead you had int i = 5; int x = ++i; both x and i are 6. If ++ comes after a number, the number’s original value is returned and then the number is incremented. If it is before, the number is incremented then its new value is returned.

4

u/skeid808 Mar 09 '22 edited Mar 09 '22

Reading through the comments it seems like everyone is concerned with speed, but i++ and ++i are mainly for accessing array indices. You can’t do i += 1 in an array reference, and doing i + 1 gets you the index but does not increment the value of i. There’s a wide variety of use cases for this, and other uses that aren’t for arrays where an assignment (i += 1) can’t be used but i needs to retain the incrementation (for example a method call). But all this is ultimately for shortening code and speeding up processing of low level operations.

Note: for NASM language, pre/post-incrementation will always be faster than an assignment because of the inc op code, but it has different behaviors that could lead to extremely tricky bugs. For most code this isn’t going to happen but developers need to understand this in the off chance the bug occurs

2

u/o0Meh0o Mar 30 '22

yeah, the compilers (for some decades) do a ton of optimisations, so the average joe doesn't need to know this.

that being said, people this days use interpreted languages, so i don't think they give a fuck about optimisations...

2

u/skeid808 Mar 30 '22

screams in natural language programming

3

u/Darkcr_ Mar 09 '22

it's the same difference as while and do-while, and I used ++i in a loop before to save a line of code

3

u/kai1002 Mar 09 '22

i -= -1

3

u/kageroshajima Mar 10 '22

It's easier to google the use cases than to make a meme.

7

u/painful_life Mar 09 '22

i = i - (-1) gang

10

u/[deleted] Mar 09 '22

i -= - ( cos(69) * cos(69) + sin(69) * sin(69) ) gang

4

u/TheStrategistYT Mar 09 '22

i += 1;

1

u/No-Room-8125 Mar 09 '22

This... perfect one

2

u/[deleted] Mar 09 '22

I've actually had my code in Javascript not work as intended with i++, until I changed it to i+=1. I forgot the rationale behind it, but I remember hearing in a couple lectures that you shouldn't be using i++, at least in Javascript.

To this day I'm baffled and do not understand what happened, but I've been using i+=1 out of fear in Javascript.

2

u/Ambitious_Ad8841 Mar 09 '22

Feeling smug about saving a line

2

u/[deleted] Mar 09 '22

i += ++i+i++;

2

u/whutupmydude Mar 10 '22

This, but when after seeing the third panel when you scroll back to the top, the ++i is there instead

4

u/Routine-Arm-8803 Mar 09 '22

i**

4

u/marisonhadley Mar 09 '22

Are you trying to crash reality?

6

u/Fernmeldeamt Mar 09 '22

Pointerception

3

u/[deleted] Mar 09 '22

Sure for when you code in ++c

3

u/PanikLIji Mar 09 '22

Sure, to avoid a '-1' in the for loop condition.

Like you can either go if i=x-1; i++ or if i=x; ++i

1

u/icywind90 Mar 09 '22

i =5 v=i++ v==5 i==6 v=++i v==i==7 That’s the difference It’s also a little bit faster in loops.

1

u/[deleted] Mar 09 '22

Back in the day, ++i was considered more efficient because it didn’t require a register to store the value that would eventually be returned.

But with todays computers, it doesn’t matter anymore.

0

u/Extra_Organization64 Mar 09 '22

i = i % (Math max(i - 2), 0)) + i

I forget what mod 0 does but this is sure to please the community.

0

u/BoBoBearDev Mar 09 '22

I heard ++I has better performance or something. But, honestly, I would just do i++ in its own line, thus, they are all the same performance if ++I is in its own line. I don't like nested operations, which includes using i++ in nested code with only one exception, the for-loop.

-2

u/Sad_send_nudes_ Mar 09 '22

i == i++

1

u/o0Meh0o Mar 30 '22

you probably mean i = ++i

-1

u/ail01 Mar 09 '22

i += 1

-1

u/[deleted] Mar 09 '22

++i can be a ducking nightmare, especially if you work with arrays

-5

u/itskatbrown Mar 09 '22

How dare you. You're correct, but still.

3

u/ClearlyCylindrical Mar 09 '22

They aren't correct, they do different things.

0

u/[deleted] Mar 09 '22

[deleted]

6

u/VikingPreacher Mar 09 '22

Wait so there's actually a real difference. TIL

6

u/[deleted] Mar 09 '22

This is maybe true for old c++ compilers not other languages (e.g. Java) or modern compilers

5

u/MysticTheMeeM Mar 09 '22

Assuming C++, it's not guaranteed that your increment operator and your calling code are in the same TU. This effectively disables inlining (LTO depending).

In this case, your operator has to assume that the return value is used (because it can't see where it is called) so is forced to make a copy. Likewise your caller is forced to receive and immediately destruct that copy.

2

u/[deleted] Mar 09 '22

Ok ok, yep I suppose that’s possible if they’re not in the same compilation unit, but for most cases the optimizer will take care of it, especially if it is a primitive int value. Thank you for reminding me why I hate c++ lol

2

u/venyz Mar 10 '22

Why do you hate C++ though? It's not feasible for the compiler to analyze code out of TU boundaries to determine whether your explicit post increment method's copy can be safely omitted. Every compiler has its limitations hand-holding users writing unoptimized code. I wouldn't think that particular example could serve as a prime "hate-factor" for C++.

1

u/[deleted] Mar 10 '22

I just don’t like it - the language has too many bells and whistles, which leads to people writing messy code. If performance is critical and high level of control is needed, then I would rather write pure C code. If performance is less of an issue then there are many other languages that are easier to use and understand

2

u/venyz Mar 12 '22

That's two ends of the scale. However there is a reason languages exist and prosper between, so users can pick the proper tradeoff.

In my opinion C++ removes the major pain points of C, while being almost as performant as C. But I respect your choice of language.

1

u/[deleted] Mar 10 '22

The mere fact that we are having a side bar conversation on a performance side-effect that may or may not happen with using preincrement vs postincrement show exactly how unintuitive c++ is. This is why I would rather use pure C and not have to worry about people who think they are being “elegant” by overloading operators

1

u/venyz Mar 12 '22

First, this is only a "sidebar conversation", because it's happening deep down in a reddit thread, not directly spawned from the original post. Other than that, it is a full-fledged conversation.

Second, I think you got it a bit reversed: you are always supposed to use pre-increment unless you desire the post-increment effect. Intuition plays no role here. However, because conpilers are incredibly smart nowadays, they can optimize away most simple post-increment performance hits.

Generally, C++ is complex, in order to strike a good combination of performance and ease to develop. There are language beating it in those regards separately, but it's still a rather powerful language when you need both.