78 points

I like this bit at the end:

As a side note, the program is amazingly performant. For small numbers the results are instantaneous and for the large number close to the 2^32 limit the result is still returned in around 10 seconds.

permalink
report
reply
14 points

Really makes you question your sanity when optimizing jumps in code without benchmarks.

permalink
report
parent
reply
15 points

For a long time I’ve been of the opinion that you should only ever optimize for the next sucker colleague who might need to read and edit your code. If you ever optimize for speed, it needs to be done with massive benchmarking / profiling support to ensure that the changes you make are worth it. This is especially true with modern compilers / interpreters that try to use clever techniques to optimize your code either on the fly, or before making the executable.

permalink
report
parent
reply
7 points
*

The first rule of optimization: Don’t do it
The second rule of optimization: Don’t do it yet (experts only)

permalink
report
parent
reply
1 point

I’m absolutely on-board …in application code.

I do feel like it’s good, though, when libraries optimize. Ideally, they don’t have much else to do than one thing really well anyways.

And with how many libraries modern applications pull in, you do eventually notice whether you’re in the Python ecosystem, where most libraries don’t care, or in the Rust ecosystem, where many libraries definitely overdo it. Because well, they also kind of don’t overdo it, since as a user of the library, you don’t see any of it, except the culmulative performance benefits.

permalink
report
parent
reply
8 points
*

I’m still mad he didn’t use the size of the number to tell the system which block to read first. I feel like that would be a great use of division or maybe modulus?

permalink
report
parent
reply
7 points

I just like how he used “% 2” in the Python code he used to generate the C++ code.

permalink
report
parent
reply
60 points

Now we just need to someone to package it and upload it to NPM.

permalink
report
reply
7 points
*

What’s another 40 gb of node_modules anyway

permalink
report
parent
reply
42 points

Could be easily made 50% space saving by only iffin all odds and return even on else. Maybe one if before to handle overflow to avoid wrong even if over the last if.

permalink
report
reply
41 points

Well yeah, if you allow cheating!

permalink
report
parent
reply
18 points
*

Yeah but then ALL even numbers would be slow to compute because you would have to chain through every odd before you know that 2 is even.

Depends on the expected distribution of input values

permalink
report
parent
reply
1 point

Heuristic: keep it until 512, afterwards powers of 2, and numbers like 1000, 2000,…, 10000, 20000,… (regex: [0-9]000+)

permalink
report
parent
reply
33 points

Let’s be real though, everything is IF statements all the way down

permalink
report
reply
27 points

There’s not a single thing in this universe that cannot be accomplished with enough IF statements… as long as you’ve got infinite time to wait

permalink
report
parent
reply
15 points
*

…you mean IF you’ve got infinite time to wait?

permalink
report
parent
reply
4 points

…you mean if you’ve got IFinite time to wait?

permalink
report
parent
reply
2 points

The problem with if is the answer comes from user. There’s no mathematical reason or scientific explanation, only programmer who thinks the answer should include the subject.

permalink
report
parent
reply
2 points

True…

But even on a more metaphorical level, every single thing that has or will happen in this universe, down to even the smallest quantum fluctuations could be encapsulated into IF statements as long as you had enough of them.

permalink
report
parent
reply
2 points

What if there was an unintentional infinite loop in your code. You could be waiting for infinite time only to learn the code had a bug. D:

permalink
report
parent
reply
30 points

This is poetry.

My favourite part is that he uses the modulo operator in his Python script to generate the C code.

permalink
report
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 7.1K

    Monthly active users

  • 954

    Posts

  • 36K

    Comments