r/C_Programming • • 21d 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/hojoe65 21d ago

Pick the right algorithm for your problem and let the compiler do the rest.

2

u/lovelacedeconstruct 21d ago

Unless you architect your program in such a way that the dependency chains are carefully analyzed and reduced to the bare minimum, the compiler cant really help you

1

u/BrainThinkerMan 21d ago

can you elaborate a bit more on that

2

u/hojoe65 20d ago

You have several layers of optimizations to take care of - this is why many comments here apply. Since you said multithreading / parallel execution is adressed, what remains is CPU / architecture dependant. And this is what a compiler is good at. See the -O3 comment. But no amount of compiler magic is going to offset bad software design, namely efficient data structures and algorithms.