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

aw27

  • Guest
Re: Need for Speed - C++ versus Assembly Language
« Reply #60 on: May 20, 2017, 04:32:45 PM »
Please rectify your popularist code project award winning post.
Yes, Sir!   :icon_redface:

K_F

  • Member
  • *****
  • Posts: 1673
  • Anybody out there?
Re: Need for Speed - C++ versus Assembly Language
« Reply #61 on: May 20, 2017, 10:46:40 PM »
I'm sure if you get rid of the high level constucts (.If .. then.. else.. etc), you see a vast improvement in speed  ;)
As the C++ compiler uses every trick in the book.. try the same for Asm  ;)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

jj2007

  • Member
  • *****
  • Posts: 11584
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #62 on: May 21, 2017, 12:10:01 AM »
I'm sure if you get rid of the high level constucts (.If .. then.. else.. etc), you see a vast improvement in speed  ;)

I've never seen such an improvement. Can you post an example?

K_F

  • Member
  • *****
  • Posts: 1673
  • Anybody out there?
Re: Need for Speed - C++ versus Assembly Language
« Reply #63 on: May 21, 2017, 01:26:30 AM »
I haven't got an example offhand, but Dedndave (or someon else) posted something on this topic a few years back.

Their example improved the 'goto' by one instruction per IF IIRC - so this was my thought as AW27's asm example had a few levels of IFs.
A couple million extra instructions could make a difference in those totals.
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Need for Speed - C++ versus Assembly Language
« Reply #64 on: May 21, 2017, 02:05:03 AM »
If you are serious about such things, you use a table of labels and reach every option with a couple of instructions.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 11584
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #65 on: May 21, 2017, 02:18:20 AM »
I am always curious to see highly optimised code, therefore some time I created the CodeSize macro. The attached testbed is purest Masm32 code, no MasmBasic, promised. All you have to do is write code that is more efficient than the built-in HLL stuff, and add a label before and after. Example:

Code: [Select]
Man_Repeat_s:
@@:
dec ecx
jne @B
Man_Repeat_endp:
CodeSize Man_Repeat

Output:
Code: [Select]
3        bytes for Man_Repeat
Really, extremely easy to use. And it's fun to outperform the HLL stuff :t

aw27

  • Guest
Re: Need for Speed - C++ versus Assembly Language
« Reply #66 on: May 21, 2017, 02:50:13 AM »
I'm sure if you get rid of the high level constucts (.If .. then.. else.. etc), you see a vast improvement in speed  ;)
As the C++ compiler uses every trick in the book.. try the same for Asm  ;)
The variation I assembled with ML64, does not contain high level constructs and has no noticeable performance differences. But I agree that the high level constructs contain more instructions.

HSE

  • Member
  • *****
  • Posts: 1765
  • <AMD>< 7-32>
Re: Need for Speed - C++ versus Assembly Language
« Reply #67 on: May 21, 2017, 02:55:13 AM »
JJ: Why not
Code: [Select]
mov ecx, 9
Loop_s:
loop Loop_s
Loop_endp:

Epa! (forget the question)
Code: [Select]
146665    cycles  for Loop
98486     cycles for jne

jj2007

  • Member
  • *****
  • Posts: 11584
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #68 on: May 21, 2017, 03:13:35 AM »
But I agree that the high level constructs contain more instructions.

For example?


JJ: Why not
Code: [Select]
mov ecx, 9
Loop_s:
loop Loop_s
Loop_endp:

Epa! (forget the question)
Code: [Select]
146665    cycles  for Loop
98486     cycles for jne

Valid example, thanks, it's indeed one byte shorter ;-)

I've added your suggestion to testbed version 2. Including the HLL equivalent :bgrin:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Need for Speed - C++ versus Assembly Language
« Reply #69 on: May 25, 2017, 09:13:35 PM »
I always laugh at some of the notions of speed, would it really matter if your MessageBoxA was a few picoseconds faster than someone elses ? You put the effort where it does matter, where you have processing bottlenecks, where time critical code is holding up the works, the rest is write it clearly, make it maintainable and reliable. Then there is the choice of algorithm, a sloppy quick sort will outperform a brilliantly optimised bubble sort, don't waste your effort on the wrong idea.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 11584
  • Assembler is fun ;-)
    • MasmBasic
Re: Need for Speed - C++ versus Assembly Language
« Reply #70 on: May 25, 2017, 10:51:37 PM »
I absolutely agree, Hutch. 100% 8)

The last question was, however, whether HLL produces longer or slower code. And I am still waiting to see an example that confirms this statement. One example would be enough.

Raistlin

  • Member
  • ****
  • Posts: 531
Re: Need for Speed - C++ versus Assembly Language
« Reply #71 on: May 29, 2017, 03:48:34 PM »
Code: [Select]
I always laugh at some of the notions of speed, would it really matter if your MessageBoxA was a few picoseconds faster than someone elses ?
Although I agree - I have another reason for doing full ASM apps. Wait for it.... TA DAAAAA = Size !

Yes, size does matter - especially when you want to create cross hardware/software SKU -platform support / installs, network-comms etc.

It does not detract either that: Smaller, generally (yes, I know that's relative) means faster.
RE: smaller in-memory footprint, cache hit probability, hdd binary size reduces load times etc....

[EDIT: Note to self - do not use "etc" so much - either list all or list none]
Are you pondering what I'm pondering? It's time to take over the world ! - let's use ASSEMBLY...

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Need for Speed - C++ versus Assembly Language
« Reply #72 on: May 29, 2017, 08:45:30 PM »
The reason that sticks in my head is POWER, size is useful in that it allows you to do things that would be clunky and complicated in a HLL not designed for the purpose which means that you can trade size for speed when your code is small enough to get away with it but below all of the practical considerations, it is the architectural freedom to design what you like in whatever way you like that is the real reason why I write in x86 assembler.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

felipe

  • Member
  • *****
  • Posts: 1367
Re: Need for Speed - C++ versus Assembly Language
« Reply #73 on: May 30, 2017, 03:27:18 AM »
Other reason, that i like is the close relation with the circuits (the microprocessor). Electronic stuffs are fascinating i think, so write in assembly is the best for controlling by software all that circuitry.  :bgrin:

jimg

  • Member
  • ***
  • Posts: 468
Re: Need for Speed - C++ versus Assembly Language
« Reply #74 on: May 30, 2017, 04:22:32 AM »
While I agree with you Felipe, unfortunately, everyone is using C to program their microprocessors these days.  Very disheartening.