Author Topic: Need for Speed - C++ versus Assembly Language  (Read 3190 times)

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Need for Speed - C++ versus Assembly Language
« on: April 19, 2017, 03:42:37 PM »
Yesterday, I published an article at Code Project. "Need for Speed - C++ versus Assembly Language"

guga

  • Member
  • ****
  • Posts: 826
  • Assembly is a state of art.
    • RosAsm
Re: Need for Speed - C++ versus Assembly Language
« Reply #1 on: April 19, 2017, 04:48:15 PM »
Hi Aw.

Out of the thread. I´m updating your code on Transposing matrix to optimize it. The version is a bit slow, due to the heavy usage of push/pop, add instructions. I´m trying to rewrite it to work at the proper speed.

So far, i succeeded to double the speed gain, removing the loops and addition operations at edi. I´m currently trying to make the same for esi and later will review the remainder. loops.

On my PC the old speed was something around 438 ns on the original version. Now it is only something around 220 and i believe it can reach something around 130 if i succeed to make the same for esi.

Once i finish i´ll post it for you.
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

LordAdef

  • Member
  • ***
  • Posts: 264
Re: Need for Speed - C++ versus Assembly Language
« Reply #2 on: April 19, 2017, 06:04:32 PM »
My guys will avenge this heresy!! C++ cannot beat a really optimized asm.

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: Need for Speed - C++ versus Assembly Language
« Reply #3 on: April 19, 2017, 06:34:37 PM »
Out of the thread. I´m updating your code on Transposing matrix to optimize it.

It is an excellent exercise.  :t
And there is also room to optimize the Fast Matrix Flip.

Siekmanski

  • Member
  • *****
  • Posts: 1145
Re: Need for Speed - C++ versus Assembly Language
« Reply #4 on: April 19, 2017, 07:57:56 PM »
LordAdef is right, no compiler can beat 100% well optimized assembly code.
Is this a challenge ?

It's the same story over and over again on the c++ forums.

- you must be mad programming in asm.
- asm is ancient and dead.
- nobody uses it anymore.
- why using it if a compiler does a greater job.
- etc.

Some of those guys are really sneaky and provoke you to write a faster routine for their own use.  :biggrin:

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: Need for Speed - C++ versus Assembly Language
« Reply #5 on: April 19, 2017, 08:35:26 PM »
LordAdef is right, no compiler can beat 100% well optimized assembly code.
I used to hear the same think about chess. No computer will ever be able to beat a chess grandmaster because computers have no ideas, no creativity, no positional sense, etc.
Now, every grandmaster is beaten by his smartphone running a free chess app with ELO 3000.

Is this a challenge ?

Sure, I know you are good at optimization, give it a go!



jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #6 on: April 19, 2017, 09:24:42 PM »
I used to hear the same think about chess. No computer will ever be able to beat a chess grandmaster because computers have no ideas, no creativity, no positional sense, etc.

I understand the logic, but the cases are a bit different: Chess computers win with brute force. Now I wouldn't exclude that one day compilers test various options for an innermost loop to find the fastest encoding, but I wouldn't bet on it 8)

Siekmanski

  • Member
  • *****
  • Posts: 1145
Re: Need for Speed - C++ versus Assembly Language
« Reply #7 on: April 19, 2017, 09:29:41 PM »
That's comparing apples with oranges.

A chess player has to respond to the actions of his opponent. ( human or computer )
A programmer already knows how the CPU will respond.

Sorry, no time to take the challenge.

The first thing i would do, align the code loops and align the data memory and move the memory allocations outside the loops.
Or better allocate the needed amount of memory once.

mineiro

  • Member
  • ***
  • Posts: 365
Re: Need for Speed - C++ versus Assembly Language
« Reply #8 on: April 19, 2017, 09:52:40 PM »
hello sir aw27, I read your article.

Processors can beat humans on a brute force way, sequential logic, but when we start inserting paralell logic we can beat machines.
Remove chess program database (chess openings) and you see how easy is to win any chess program with high ELO level. Chess programs use database created by humans, by games between humans IM or GM. Have more chess possible movements than stars on our universe, so, how much time a computer chess program will spend to only do the five first moves without a database and using only chess rules as support?

I'm with opinions from persons here, c++ code can't beat an experienced assembly programmer. If we consider that a compiler was done by a lot of persons, going from math point of view reaching opcodes, let's do the same, let's join experienced assembly programmers to work as a team and this way I know the final answer. I'm saying this because we have much more freedom against c or c++ programmer.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: Need for Speed - C++ versus Assembly Language
« Reply #9 on: April 19, 2017, 10:29:55 PM »
Chess computers win with brute force.
The problem is that they don't win anymore by brute force alone. It would be impossible because there are trillions of possibilities.

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: Need for Speed - C++ versus Assembly Language
« Reply #10 on: April 19, 2017, 10:36:37 PM »
Remove chess program database (chess openings) and you see how easy is to win any chess program with high ELO level.
Grandmasters have also encyclopedic knowledge on chess openings. On the middle game, is where computers take the lead, not to mention on finals.

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #11 on: April 19, 2017, 10:53:05 PM »
Yesterday, I published an article at Code Project. "Need for Speed - C++ versus Assembly Language"

Nice work, you put a lot of effort into it :t

Typo in point 7): In order words,... you meant in other words, I suppose.

Quote
I undertook a few optimization steps with the ASM source code and was able to improve the assembled performance by more than 30%.
...
Quote
I have seen the ASM listing produced by the C++ compiler and some parts are just mind blowing - nobody would believe a human would code that way (if he does the code would be almost impossible to maintain). The compiler uses every trick under the table in an automated way - difficult to beat. It knows about everything about how the pipelines and predictive branching work, it reorders of instructions, does loop tiling and uses cache-oblivious algorithms.

Of course, you have a point there. On the other hand, if you take the compiler's assembler listing and play around with it, you may tickle out a few % more... and prove again that assembler is faster ;-)

Just joking. Your article clearly shows that a C++ compiler can beat assembler. However, truth is also that in the Lab we have beaten the CRT many times, typically by a factor 2-3; and one would assume that the CRT developers use the best compilers, no?

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: Need for Speed - C++ versus Assembly Language
« Reply #12 on: April 19, 2017, 11:20:57 PM »
Typo in point 7): In order words,... you meant in other words, I suppose.
Thanks. I have a few more to fix.

Quote
if you take the compiler's assembler listing and play around with it, you may tickle out a few % more... and prove again that assembler is faster ;-)
In a real case I would feel tempted to compile the compiler's assembler listing  :biggrin:

Quote
CRT many times, typically by a factor 2-3; and one would assume that the CRT developers use the best compilers, no?

The initialization code looks at lots of things we typically don't.

LordAdef

  • Member
  • ***
  • Posts: 264
Re: Need for Speed - C++ versus Assembly Language
« Reply #13 on: April 20, 2017, 04:05:26 AM »
The article is really well written, congratulations.

But the the asm code can be further optimized. Marinus mentioned the memory allocation, but there are those PUSH-POPs, there is the shl which is slow. If I'm not mistaken lea is slower too and could be exchanged to mov - offset (can that be done in 64?).

What I'm saying is that a guy like Marinus, Johen and others could make this asm run faster than the C++ one. In this case, the point of the article looses ground.

Another thing to consider is, if one refers to the article's title, when you really do the C++ thing (oop, those C++ crazy abstractions, etc...), C++ gets increasingly slower and slower. It's slower than pure C.

My only point here is that you are comparing a fully optimized C++ code versus a not fully optimized asm.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4930
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Need for Speed - C++ versus Assembly Language
« Reply #14 on: April 20, 2017, 05:01:25 AM »
This much I have seen with comparisons of this type, once the optimisations on both sources are done if the code is competent in both, if the same instructions are used in both, the difference is negligible. Where the real action will be is in designing better algorithms and parallel multi-thread applications as SSE and AVX instructions are not particularly responsive to minor twiddling like the older traditional integer instructions. You do the obvious things like not repeatedly running code that should safely exist outside intensive loops, align all of the data to its required optimums and you can have a fiddle with code alignment if you think it can make it a bit faster but the action here will always be better design, not close range twiddling.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: