Thread: assembly is slower !

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    assembly is slower !

    I write a short _asm, but it is very slow:

    //this is C++
    Code:
    double *pdb0 = pMatrix + ic;
    double *pdb1 = pMatrix + i;
    double dbT = ...;
    //this piece of c code is replaced by _asm as bellow
    Code:
    for(j=0; j<iOrder; j++)   
    {   
    	*pdb0 = *pdb0 + dbT * *pdb1;   
    	pdb0 += iOrder;
    	pdb1 += iOrder;
    }
    Code:
    _asm
    {
    	finit;
    	mov		edi,	pdb0;		edi = pdb0
    	mov		esi,	pdb1;		esi = pdb1
    
    	mov		eax,	iOrder;		eax = const iOrder * 8
    	shl		eax,	3;
    
    	mov		ecx,	iOrder;
    	fld		dbT;
    
    Loop_Start:
    	fld		qword ptr	[esi];	load *pd1
    	fmul	        st(0),		st(1);
    
    	fld		qword ptr	[edi];	load *pd0
    	fadd;
    
    	fstp	        qword ptr	[edi];	set to *pd0
    
    	add		edi,	eax;		add iOrder*8;
    	add		esi,	eax;		add iOrder*8;
    
    	dec		ecx;
    	jnz     Loop_Start;
    }
    above _asm works fine, but it is slower than C code.
    why and how to improve it?

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Most C compilers have a switch/flag to cause the generated asm to be saved to a file.

    Do this, and study it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    why is it so slow?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by mark9876 View Post
    why is it so slow?
    Because the compiler is better at coding in assembly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the more new and delete operation, the slower they gets?
    By idleman in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2009, 11:16 AM
  2. Gmail slower?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-27-2005, 11:26 PM
  3. computer going slower than normal
    By MisterSako in forum Tech Board
    Replies: 20
    Last Post: 08-17-2004, 03:35 PM
  4. slower loop 2nd time
    By bballzone in forum C++ Programming
    Replies: 9
    Last Post: 07-28-2004, 02:38 PM
  5. Linux's GUI slower than windows'....
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 02-16-2002, 08:47 PM