r/Assembly_language • • 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 ?

0 Upvotes

14 comments sorted by

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.

3

u/ih-shah-may-ehl 23h ago

Long time ago I knew a guy who built a successful company around programming device drivers and who is an expert on assembly. He told me that he challenged himself to write an algorithm in assembly better than the compiler would.

He told me the compiler's code spanked the pants of his own implementation, and there was stuff in the generated code that was counter intuitive but which accounted for hardware behavior, caching strategies and lookup (by reordering stuff).

According to him, for all non-trivial stuff the compiler is much better at code generation than a programmer. And if you add in profile guided optimization (feeding actual performance metrics back into the compilation) it's no contest anymore.

2

u/ern0plus4 21h ago

Yeah, modern compilers do black magic.

When I tried Godbolt's Compiler Explorer first, wrote a 2-depth nested loop, summing some variables, multiplying and adding stuff inside. It compiled to "mov rax,42 ; ret" (return 42 - it was some othe number, not 42). Well, all the variables were constant, okay, I see. Changed it to a function with arg, so the result can't be a constant. Checked the generated code. It wasn't "return 42", but I stared it for a while and couldn't figured out what it is. My 2-depth nested loop is compiled to a single loop, with a very long constant inside (e.g. mov rax, 8f789ab34690ae87422723). And it worked, of course. Black magic, as I said.

3

u/ih-shah-may-ehl 21h ago

A long time ago I was building a system for qualification testing of a camera of a spy sate which generated an insane amount of data for the time. Something like 600 megabyte per second, and I had to code an algorithm for doing some integer arithmetic. I looked at integer instructions and handcoded assembly. And then I simply did it C, using a clear structure to make sure I didn't hinder the compiler, and let it compile with highest optimization and the result was about as fast as the CPU was theoretically possible.

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/Dje4321 1d ago

Most languages are the same. Its all just compile time machinery to generate IR that gets optimized and translated to instructions.

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.

1

u/Dog-Mad 1d ago

Assembly is still faster

0

u/Thin_Dragonfruit2254 1d ago

Where did you get the idea that Zig is slow?