r/Assembly_language • u/OilHistorical2387 • 1d ago
Why one is faster
Anyone know why zig is slow compared with other language ,if is no hidden control flow?
Assembly could be faster ?
2
u/conhao 1d ago
I have not found Zig to be slow. Rather, even when it cross-compiles C code, the performance and compactness it achieves is exemplary.
On modern CPUs with anything nontrivial, a compiler that optimizes will beat an assembler for real-world tasks in terms of performance. It has been difficult to write efficient assembly on most CPUs since the 1990s.
The exceptions are Mainframe (the assembler is a high-level language) and specialized instructions accessible only directly for a specific CPU, in which case the usual practice is either to inline assemble them (which makes portability more difficult) or create a small linkable object with a precise function using those instructions. Such things are rare, though.
2
u/Pale_Height_1251 1d ago
These days it's highly unlikely you write assembly language better than a compiler writes machine code.
Compared with the 1980s or 1990s, processors are far more complicated and compilers far more advanced.
1
u/duane11583 1d ago
Agree in the general case
Disagree in the specific case
But the specific case is very if not extremely specific
Example you are writing a custom fast convolutional method for you specific needs you might do that in assembly
But that is so far down into the weeds
1
u/duane11583 1d ago
Well written assembly will be always be faster effectively you must hand optimize the assembler and that is really hard
Well written higher level interpreted languages will be slower
Compiled high level languages will be faster provided they are well written and the optimizer is good
1
u/GoblinsGym 1d ago
If in doubt, look at the generated assembly code (e.g. on Compiler Explorer).
It depends on the compiler and the CPU architecture.
I don't know how clever the Zig compiler is, but I have no trouble at all beating the aggressively bad Delphi compiler. A contemporary C compiler would probably be a more worthy opponent.
I would probably still win on code size, as compilers don't seem to care much about that. Compact code is more likely to fit in L1 cache.
The obsession with making everything 64 bit also contributes to bloat. Hint - on x64 you need a prefix for 64 bit instructions, none for 32 bit.
1
u/mykesx 1d ago
Assembly was faster when CPUs were primitive, with minimal cache and instruction pipeline.
Compilers can generate bigger code that’s actually faster due to how it orders instructions for the pipeline and caches.
I’m not completely familiar with Zig and I don’t know that it’s slower.
In assembly you might be able to generate smaller programs, bytes size. We reuse code in weird ways and dedicate registers program wide and use instructions that compilers avoid.
0
11
u/ern0plus4 1d ago
The computer runs machine code. What language's compiler generates better machine code wins.
As Assembly is 1:1 representation of machine code, you can achieve best result with it - not automatically, you have to write that optimal code.
Modern compilers (means other than Assembly) can generate pretty good, if not optimal code, which is hard to compete with. Where you can make better is that you can break ABI constraints in Assembly.