this post was submitted on 29 Dec 2023
22 points (89.3% liked)

General Programming Discussion

7803 readers
1 users here now

A general programming discussion community.

Rules:

  1. Be civil.
  2. Please start discussions that spark conversation

Other communities

Systems

Functional Programming

Also related

founded 5 years ago
MODERATORS
 

If you use a compiled language, you should periodically look at Godbolt and see what your code is doing and what changes to your code will do in the compiled output.

In this case a positively insane way of calculating squares and cubes generates 311 lines of ARM assembler output that will swallow your memory. With even something as simple as -O1 on the command line it's replaced by one or two multiplications respectively. With -fwhole-program it removes the functions entirely and interlaces them into the loop in main().

Know your tools. It makes huge differences!

you are viewing a single comment's thread
view the rest of the comments
[–] mo_ztt@lemmy.world 5 points 10 months ago (5 children)
int cube(int num) {
    char rv[num][num][num];
    return sizeof(rv);
}

"Doctor, it hurts when I do this."

[–] r2p2@lemmy.world 2 points 10 months ago (1 children)

Not sure if op is trolling. Seems to be at the same level of sleep sort. (But there not even compiler optimizations can help.)

[–] ttmrichter@lemmy.world 0 points 10 months ago (1 children)

Not trolling. Just:

  • showing how clever optimizers can get these days
  • introducing a cool web site
  • highlighting the importance of occasionally peeking under the hood to spot gross inefficiencies
[–] mo_ztt@lemmy.world 1 points 10 months ago* (last edited 10 months ago)

Eh. Honestly, I think what you're saying, and the points the article is making, are pretty valid. That's still gonna be way, way overshadowed by the absolutely ridiculous example they chose to use to make their point. Like "Since you're writing code that's ridiculous to such a degree that it wouldn't even occur to most people that the way you're doing it would even work, you better turn optimizations on, so the compiler can fix your code back to normalcy behind the scenes for you."

load more comments (3 replies)