NO

Author Topic: Slowdown of C code when inline assembler is used  (Read 6364 times)

kobold

  • Guest
Slowdown of C code when inline assembler is used
« on: February 08, 2009, 06:08:19 PM »
Hi again! After a long period of inactivity i am back again.  :D

Today i discovered a strange behavior. And that's why i wrote this topic.

In a test application i was using inline assembler to make a benchmark between C and ASM.
The ASM part was several times faster (to my surprise - because i am not an experienced ASM coder).

But now there is a thing i do not understand:
The speed of the C part increased dramatically (about between 2 and 4 times) when i commented the ASM Parts out.
Suddenly the C part reached or beaten the speed of the ASM code!

More strangeness:
Even when i include only some sort of ASM nonsense code somewhere in the C code, the behavior was the same.
For example i inserted this at the end of the program before the "return(0);":
Code: [Select]
_asm push ax
_asm pop ax
or even better ;) :
Code: [Select]
_asm nop

As a result the speed of the C code dropped dramatically again.

Settings: Console application, x86, no debugging.
The problem occurs only if some sort of compiler optimization is activated.
Without optimization the speed stays the same - with or without inline ASM.
My version(s):
Code: [Select]
RSRC0007.DLL: Version 5.00.2
SUPPORT.DLL: Version 5.00.1
POCFMT.DLL: Version 5.00.0
PORC.DLL: Version 5.00.0
POBR.DLL: Version 5.00.1
SQLITE3.DLL: Version 3.5.3
POCC.EXE: Version 5.00.11
POASM.EXE: Version 5.00.3
POLINK.EXE: Version 5.00.0
IDESPAWN.EXE: Version 5.00.0
« Last Edit: February 08, 2009, 06:11:06 PM by kobold »

Online Vortex

  • Member
  • *
  • Posts: 870
    • http://www.vortex.masmcode.com
Re: Slowdown of C code when inline assembler is used
« Reply #1 on: February 09, 2009, 07:07:02 PM »
Hi kobold,

Inline assembly can interfere with the C code optimization. You should move the assembly routine to a separate module, assemble it with Poasm and then link the object module with the main project module.
Code it... That's all...

kobold

  • Guest
Re: Slowdown of C code when inline assembler is used
« Reply #2 on: February 10, 2009, 07:15:44 PM »
Ok - i'll try it :)