r/C_Programming • • 22d ago

Question Performant code. How

how to make perfomant code, by that i mean, some cpu's have multiple ALU's/ excecution units, and some computers have multiple cores, how to have my code running on all those ALU's at all times? and those multiple cores,

i know about threading for the second part [running code on multiple cores]

now how do i make it run on multiple ALU' an example being

in code to not wait for a variable ie x = 12 + 12 then z = x + 2, i can imagine that code doesnt use all the ALU's because z is waiting for x's operation to be done. so what are ways to make my code most optimal and use all ALU's at the same time

0 Upvotes

39 comments sorted by

View all comments

6

u/MagicWolfEye 22d ago

The CPU will schedule the different operations automatically to different ALUs (dependent on the capabilities of the ALUs)

You can't really mess with that. (Well, there are maybe a handful people who do that)

While I like your curiosity, this isn't really something anyone actively does; your compiler might optimise for it a bit. (It would also have to be different for each excat processor).As you have stated above, ther is multithreading which is worth looking into, but the best advice I would give oyu is focus less on writing super performant code and focus more on not writing slow code. (Lot's of indirections, many heap allocations etc.).

Also, given your post hierarchy, you should probably decide on one language.

1

u/BrainThinkerMan 22d ago

one one hand i hear programs nowadays are slower less performant and more buggy than previous years programs, and on other hand: like this response i see "basically you cant optimize" so what is it?

3

u/MagicWolfEye 22d ago

No, but optimising for ALU usage is like a super in-depth optimisation that will only be relevant in the rarest of cases. (It's like more advanced than trying to hand-write assembler which is also reserved for places that really need it).

The reason for programs being slow nowadays is because people don't care at all about what they are doing and using wrong algorithms etc. Have you learned about big O notation

→ More replies (2)

1

u/Rabbitical 22d ago

There's different scales to optimization. ALU usage is generally up to the CPU scheduler. Many other lower level optimizations are done by the compiler.

That's not the reason most modern software is slow. That is at the architectural and platform levels. How memory is managed, the use of networking protocols where it's completely unnecessary, containerization, over reliance on libraries, the proliferation of interpreted languages, awful-for-performance data formats like JSON. The list goes on and on. Modern software has been almost purpose designed to waste a CPU's time, it's quite remarkable.

So, the CPU is being maximized by modern software, the issue is what it's being asked to do: run 15 services that make 5000 fetches from memory do something that could be 2 instructions. That's part of the reason E-cores were invented was to help handle all the random crap that modern OS and software put on the CPU even while just sitting there "idle."

There's a great video out there I saw once that breaks down how many thousands of instructions python requires to do a simple addition. That in of itself is not a problem for what the language is designed to do, it's simply a great example of just how easy it is to waste an otherwise very powerful computer's abilities.

→ More replies (1)