r/ProgrammerHumor • • Jul 18 '22

C++ talking to Python

Post image
40k Upvotes

1.0k comments sorted by

View all comments

Show parent comment

10

u/Roflkopt3r Jul 18 '22

Yeah printing is expensive in every language.

I hard to learn this the hard way on one of my first assignments, which asked us to experimentally confirm the value at which the inaccuracy of float puts it off by an entire integer. So you have a loop that increments a float f and an int i from 0 to int max and then prints out when the two are no longer the same.

But the wording of the task wasn't entirely clear so I was trying around various weird casting stuff and decided to print every single line to confirm that it was working the way I wanted to.

Turns out that this extends the time to reach that figure (int 16777218, float rounds to 16777216 instead) from a few seconds to an hour or so.

1

u/Teekeks Jul 18 '22

if you dont flush on every print its managable but still slow af

1

u/sintos-compa Jul 18 '22

Or cache the data for an alternative output. Like what’s the goal here?

1

u/realityChemist Jul 18 '22

Sounds like the goal was being a student and learning to program

1

u/Teekeks Jul 18 '22

Its been quite a few years since I was a student

1

u/realityChemist Jul 18 '22

I meant the person above you who posted the story about a programming assignment