r/cprogramming • u/sourav_bz • Oct 23 '26
At what point writing direct assembly program is more efficient than C program?
/r/Assembly_language/comments/1xd8n5s/at_what_point_writing_direct_assembly_program_is/
8
Upvotes
11
u/EpochVanquisher Oct 23 '26
As a rule, almost never. Writing an assembly language program is very slow, and it’s much more efficient to write in a high-level language.
The program itself will be more efficient, but that’s rarely a good justification.
7
u/LunaticDancer Oct 23 '26
Unless you're a top 0.01% programmer specialized in low level code, you're not writing Assembly code that's more efficient than the C/C++ compiler output. The only point of writing raw Assembly is to either learn or have fun.
6
u/non-existing-person Oct 23 '26
It's not "a point" where it's faster. Vast majority of time compiler will do better work than you with optimizations.
It's just that there are some cases where assembly is more beneficial. That's it.
And as always - profile before doing any optimization at all. First focus on good design. You can screw up your performance or boost it heavily just with good or bad design. Like, understanding how you access your data so you maximize cache hits.
1
u/Nothing_from_void Oct 24 '26
A lot of dated info here; writing assembly is quite fast now, because LLMs are very good at it. I have gotten significant speed ups over libraries by just writing an assembly kernel for a specific target, it's actually quite easy to beat the compiler on when re-writing single functions. One of C's biggest strengths is the easy interop it has with assembly
2
u/orange_oki Oct 23 '26
It's pretty close, but it should be portable and therefore the compiler fail to use all the available CPU instructions, that's where you can get speed from.
Also there are special use cases with unrolling, cache lines or memory size reduction.
But the saddest point is if your work goes to the bin, or to the drawer.
So focus on portability and reusability instead of squeezing out the last bit of performance.
https://www.eventhelix.com/embedded/c-to-assembly-translation/
2
u/Key_River7180 Oct 23 '26
Terry davis/alan turint/von neumamn point, you need to be really smart to outsmart a c compiler
1
u/poya_mohammadi Oct 23 '26
Writing in C is far better than working with Assembly. While learning Assembly is certainly valuable and teaches you a great deal about technical fundamentals, building complex software—such as compilers, interpreters, or a code editor like GNU/nano in Assembly is an incredibly arduous task. Although C can be challenging in certain respects, it is, on the whole, much better than Assembly.
1
u/timonix Oct 23 '26
When you don't have a compiler
1
u/21Ali-ANinja69 Oct 23 '26
Maybe SIMD. Maybe.
1
u/theunixman Oct 23 '26
When the profiler tells you it is.
1
u/Nuclearo-Dinosauro Oct 23 '26
All programs are compiled into some sequence of instructions. If you can achieve same task in assembly in one less instruction than it's equal in C, then you are more efficient. But this kind of optimization would cost you amount of developing time that it's not worth it.
You should focus more on writing code in such a way that you can make any change at any time in short time.
Flexibility is much more important than performance.
Reduce system calls, heap memory allocations, and other time heavy operations to absolute minimum and your performance will be great.
But you can dissasembly your C programs to check what they are really doing. It's a great way how to learn and understand C.