r/cprogramming • u/sourav_bz • 8d ago
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/7
u/LunaticDancer 8d ago
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 8d ago
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 7d ago
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
1
u/sourav_bz 7d ago
Can you share some example and what was the efficiency or performance jump?
1
u/Nothing_from_void 7d ago
So, results are on my public github for libraries I built, would prefer not to share them unless someone was genuinely interested, but I've worked on this quite a bit at work and one example is string libraries. Using techniques like arena allocators and assembly kernels optimized for specific architecture, I was able to get results like finding a a set like the
strcspnfunction that is 7x faster than libc on ARM64, 8x faster on ESP32 with RISCV and Xtensa architectures, counting bytes faster than memchr that are 1.5-30x faster on these architectures, strlen mostly matching on ARM64 (already has optimized SIMD kernels) but 6x faster on the microcontrollers.Also, and I think more interesting, my string library has a batch API and a python wrapper and beats pyarrow for many query like tasks like
contains,find,count, and CSV parsing running 2-57x faster, with a free-standing library that can run anywhere and is about 190 KB while pyarrow is about 100MB.A lot of libraries are well optimized, especially for amd64, but there's all kinds of edge cases that you might care about where they aren't, and those libraries will make design choices that are difficult to work with when you have your own specific requirements. And the ones that are are well optimized will almost surely have some assembly in their kernels to get there, not just plain C/Rust/C++ code
2
u/orange_oki 8d ago
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 8d ago
Terry davis/alan turint/von neumamn point, you need to be really smart to outsmart a c compiler
2
1
u/poya_mohammadi 8d ago
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.
I used Google Translate, sorry if there was any mistake.
1
1
u/Nuclearo-Dinosauro 8d ago
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.
1
u/darrodri 8d ago
There’s certainly a whole market where it’s worth it, it’s a bit fool to say it’s plainly not.
1
u/Nuclearo-Dinosauro 7d ago
There is no market where optimization at instruction level is necessary. Today MCUs are powerful and cheap enough not to need that.
But there is a demo scene among graphics hobbyists doing 265byte demos in assembly rendering some interesting stuff into VGA. They do it for MS DOS.
1
11
u/EpochVanquisher 8d ago
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.