AsmBB

Power
Login Register

Why assembly programs are faster than HLL programs, despite that the compilers are so advanced?
0

#15506 (ツ) johnfound
Last edited: 18.06.2019 by johnfound, read: 11213 times

The paradox.

The hand written assembly language programs are faster and use less memory than the programs with the same features, but written in high level languages.

I will give below some examples from the real life. In addition I have made several artificial experiments that show the same.

This looks like a paradox, because the HLL compilers are very effective these days and for big programs generate more optimal code, than the hand written assembly language.

Yes, it can be formally proven the following:

theorem:

The hand written assembly language code, is always more optimal or equal to the compiler generated code.

This statement is easily proved by the fact, that the programmer can always read the compiler output and optimize it further, while the compiler can't do the same with the programmers code.

But this theorem is not very helpful in the real life. Simply because the compilers generate huge amount of code, that can not be manually read, analyzed and optimized by a human.

Fortunately, the assembly language programmer, does not need to compete with the compiler in the platform specific optimizations, in order to beat it.

The fastest assembler today is FlatAssembler. It is written in assembly language and is "optimized" for 80386 CPU, if this can be qualified as optimization at all. The competitors are written in C/C++ and are slower, despite of the more optimal code generated by the C/C++ compiler.

The fastest OS is KolibriOS written in assembly language. It boots for less than 2 seconds to the GUI desktop. Even the BIOS startup is slower. And the GUI is instantly responsive even on very slow and old machines.

The fastest web server is RWASA and it is the only of the above examples that is really optimized for speed. But it still uses less memory than its competitors.

Actually, in most cases, writing programs in assembly language, the programmers put more effort in writing smaller and readable code, than in writing faster code. But as a result, these programs always perform faster than the HLL equivalents.

Why?

Let see the so called "Jevons paradox". This is an economics paradox, but actually it can be applied to the programming as well.

Jevons paradox

In economics, the Jevons paradox occurs when technological progress increases the efficiency with which a resource is used (reducing the amount necessary for any one use), but the rate of consumption of that resource rises due to increasing demand.

If we apply this definition to the programming, we can see, the direct analogy. The compilers actually increases the efficiency with which the computer resources are used by the programmers. With one line of code in HLL the programmer can use more resources than with one line of code in assembly language.

The same way more efficient car have greater mileage with a liter of fuel.

As a result, the use of the computer resources CPU and RAM increases. Because of increased demand.

The same way the traveled kilometers and the total fuel consumption increases with more efficient cars.

Notice, that the increased efficiency of the compilers is not the only increased efficiency in the IT. The efficiency of the hardware also increases with the time. Now the RAM is faster and bigger than ever, the CPUs are also pretty efficient.

But all these improvements, lead only to increased consumption of the resources. In full accordance with Jevons paradox.

As simple as that.

How to counteract?

In economics, there are tools to counteract the Jevons paradox. For example, the increased taxes can stop the demand raise and neutralize the effect if the goal is to reduce the resource consumption.

But there is no taxes for "CPU cycles" or "RAM ussage".

The only way for the programmers is to consciously limit themselves from using this increased effectiveness of the HLL compilers and hardware improvements.

Someone will probably ask here "Why we should limit the resource use? The RAM is cheap and the CPU is fast."

The answer is simple and straightforward: Because we will need these resources in order to develop our programs further.

In addition, the programmers time is of course important, but the programmers often forget that the program is written once, but executed sometimes millions of times. One saved second by writing faster program, can result in millions of saved seconds for the users of the program.

Yes, the program needs maintenance and further development, but who said, the faster program needs more effort for the maintenance?

After all, the mentioned above FlatAssembler is developed and maintained for almost 20 years by a single person. I didn't noticed some enormously great effort for this program to be maintained, regardless of the fact it is written entirely in assembly language.

Of course, the programs in HLL also can be written in efficient manner. There are many examples of C/C++ projects written this way and really performing excellent.

But the higher is the level of the language, the harder is to write efficient code. In Java and .net it is almost impossible.

The future

Fortunately or not, but the overgrowth of the computer performance, actually ended. There will be no exponential growth anymore. Nor even close. We can expect some slow linear growth, or even some decline (the mobile devices) of the computers performance and resources.

So, now the programmers have the time to put their code in order and to start to pay this giant technological debt they accumulated during the big bang of the hardware.

If during the exponential growth the technical progress provided the RAM and CPU for the next program version, now this time ended.

If someone have a program that uses all resources of the computer and want to implement new features, he will be forced first to optimize the existing features to use less memory and CPU and to use the released resources for the new features of the program.

And I have some strong suspicions that this process is already silently running in most of the software companies.

But for the new projects, the economical use of the resources from the day one, seems to be better strategy, simply because it is easier to write resource friendly code from the beginning, than to rewrite it later.

BTW, more and more people start to call for more resource friendly code. I am reading such articles every day on different blogs and social networks.

So, interesting times are coming. The era of deep software optimization. :-)

#15660 (ツ) kosbar
Created 20.11.2018, read: 8827 times

Need nice book about Nasm or Fasm, have you smthng?

#15662 (ツ) johnfound
Created 21.11.2018, read: 8825 times
kosbar

Need nice book about Nasm or Fasm, have you smthng?

I don't know about NASM, but there is no for FASM. The best way I know to learn FASM is to use it:

Read some instruction set tutorial (regardless of the particular assembler, only avoid gas and AT&T syntax). Every assembly language tutorial for beginners will do the job.

Read the FASM reference, provided with the distribution. It is about the FASM syntax. Keep it close for fast reference later. (In Fresh IDE it is on Ctrl-F1 distance).

Then try to play with the examples (also in the distribution package). Ask on the forum and read the old posts. Especially the "examples" section. There are tons of good sources.

Try to change some of the examples in a way you like. Try to start your own small project. Always set goals higher than your current level.

#15731 (ツ) amoschenko
Created 22.12.2018, read: 8571 times

And what is your next move after ASMbb? :-)

#15732 (ツ) johnfound
Created 22.12.2018, read: 8569 times
amoschenko

And what is your next move after ASMbb? :-)

Why "after"? I am working in parallel on my Fresh IDE project. There is a lot of work in order to make it fully portable.

#15853 (ツ) KyberMax
Created 06.05.2019, read: 7799 times

There are still at least two advantages of Asm programming. The first advantage can be illustrated with a picture that HLL programmers troll each other.

https://cdn-images-1.medium.com/max/1600/1*C4rHvsvar3X9wPCJ-IA4tA.png

But in fact, this is almost the case when programming in Asm. And for this it is enough to be a typical Asm programmer and use a safe style. Indeed, in the debugger you can see exactly the code that is in the source code, and while you are busy with loading and setting breakpoints, you most often see an error before running the code in the debugger.

The second advantage is obvious - just look at my avatar. After all, it is obvious that a cyborg can only come to a true Asm programmer, but not to some dull trained eared HLL monkey.

#15902 (ツ) macadoum
Created 18.06.2019, read: 7518 times

Hello johnfound, when you edited the first post, this bring a new notification and up the topic to the first place. Is it intended ? (I don't think this should happen).

#15903 (ツ) johnfound
Created 18.06.2019, read: 7515 times
macadoum

Hello johnfound, when you edited the first post, this bring a new notification and up the topic to the first place. Is it intended ? (I don't think this should happen).

Hm. I would say it is "semi-intended". This way the visitors can notice that the post is edited (and I will implement later the diff algorithm in order to see what exactly has been changed). On the other hand, yes it is a little bit annoying...

Will think how is better to resolve this problem. :-)

#15996 (ツ) redsock
Created 17.01.2020, read: 6623 times

Greetings and salutations.

I see this thread every now and then in the referer logs to rwasa, much appreciated.

Over the last 6 weeks or so, I have compiled two videos to form part of my "Modern Linux Assembly Language" terminal video series, and was surprised that you didn't chime in.

Interested in your thoughts and feedback as always.

https://2ton.com.au/videos/tvs_part1/

https://2ton.com.au/videos/tvs_part2/

Cheers,

--Jeff (author of rwasa as mentioned in the original thread post).

#15997 (ツ) johnfound
Created 17.01.2020, read: 6612 times
redsock

Greetings and salutations.

I see this thread every now and then in the referer logs to rwasa, much appreciated.

Over the last 6 weeks or so, I have compiled two videos to form part of my "Modern Linux Assembly Language" terminal video series, and was surprised that you didn't chime in.

Interested in your thoughts and feedback as always.

urlhttps://2ton.com.au/videos/tvs_part1//url

urlhttps://2ton.com.au/videos/tvs_part2//url

Cheers, Jeff (author of rwasa as mentioned in the original thread post).

Hi, Jeff. Nice to see you here. Of course, I noticed your videos, but unfortunately, recently I am so busy with everything (job and family) that I can't spare some time for forums conversations and programming on my projects. I hope this rush will end sooner or later and I will be able to make what I love to make - programming in assembly and be in touch with other assembly programmers. :-)

#15998 (ツ) ganuonglachanh
Created 01.02.2020, read: 6469 times
redsock

Greetings and salutations.

I see this thread every now and then in the referer logs to rwasa, much appreciated.

Over the last 6 weeks or so, I have compiled two videos to form part of my "Modern Linux Assembly Language" terminal video series, and was surprised that you didn't chime in.

Interested in your thoughts and feedback as always.

urlhttps://2ton.com.au/videos/tvs_part1//url

urlhttps://2ton.com.au/videos/tvs_part2//url

Cheers, Jeff (author of rwasa as mentioned in the original thread post).

Hi, redsock

Thanks for your tutorial, much appreciated :-)

Why assembly programs are faster than HLL programs, despite that the compilers are so advanced?
0

AsmBB v3.0 (check-in: a316dab8b98d07d9); SQLite v3.42.0 (check-in: 831d0fb2836b71c9);
©2016..2023 John Found; Licensed under EUPL. Powered by Assembly language Created with Fresh IDE