News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

asm vs c execution speed difference

Started by negativegeforce, November 26, 2005, 05:12:21 PM

Previous topic - Next topic

negativegeforce

This is my first post on this nice forum.  I have been reading the threads on this forum for the past few months.  I am just starting to learn ASM, and it has taught me alot more then any other language could have about computers/compilers.  I bought Kip Irvines 4th edition assembly book for intel cpu's, and today I made a little comparison for fun.  I wrote a program in c, and in asm that do the same thing basicly and did a difference in time for the execution.  I was really suprised to see that the c code was actually faster consistantly on my computer.  I was just wondering if the speed hit is from Kip Irvine's library im using or is it the masm 6.15 compiler?  I was just wondering what made the asm program slower.  Anyways i look forward to post more and possibly help others in their brain frying fun.  Thanks!

http://www.ngforce.com/dev/speed.zip

Grincheux

On my notebook I got 11797 for the C program and 11188 for the Asm program.
On a short program difference does not seem to be very significant.
I would replace de loop statement by :

DEC ECX
JNZ LOOP1

I would begin the dump string with a CR/LF pair, like this I kill the CRLF call.

Try to put the counter into ESI or EDI register and replace with the foloowing lines

INC ESI
MOV EAX,ESI

And if the WriteDec could use the ESI register rather than EAX that could improve speed too.

And why to have a call to this statement ? Include it in the loop.

I am not an expert but this could help the asm program to be quicker...

Goog luck
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

drhowarddrfine

The thing to remember is that asm is next to the machine.  All others need to compile to asm to get next to the machine.  The compiled code is done automatically and can do a very good or very bad job at it.  Just like asm code can be done very good or very bad.  C code can never be faster than the same asm code because you can't get closer to the machine than asm (ignoring hex and all that).  C can only be as fast as asm.  C can only be faster if the compiler outthinks the asm programmer.  In large programs it can do this because the programmer tires and can lose track of things.  Eventually, he can get it as fast or faster but the C compiler can get there quicker.  There are some cases where the compiler can never "get there" but time must be taken into consideration.

hutch--

negativegeforce,

Welcome on board. It much depends on the task as to whether you get a speed difference between the two code forms of not. late model C compilers are a lot better than the old ones and with well understood code layouts they generally run fine and fast enough. Something that is very useful to you when you are looking for more performance is to set the C compiler to asm output without any optimisation then manually optimise the code until its as fast or faster than the C code.

Assembler programming is still an art and the more you know, the better the results you get and the advantages are twofold, get good at optimising code and you get better performance where you need it but you also start to understand the machine better and you generally write better high level code as well. Once you have enough practice you start to win with orders of complexity. Anything can produce a fast single loop because the techniques are well understood but as the code becomes more complex with multiple nested loops or interdependent loops, assembler starts to pick up advantage where a compiler starts to get into trouble.

The other end is assembler can produce very small code when it is required and this is another factor when speed is not the only issue. Optimise for speed where it is needed and optimise for size where it is not and you get overall performance that a compiler cannot deliver.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Infro_X

hutch, the gcc compiler also has optimizations for size, same rules apply though, asm programmer can get it better if he works at it, but that costs time.

hutch--

Infro_X,

I have no doubt that GCC works fine and has the optimisation range available but it is as you say, you have to work at it to get the results. The method that makes sense to me is keep 2 classes of files to build for the project, one set of files that are optimised for size and where you have speed critical tasks, another set that are fully optimised for speed and you get the main advantage of both.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

EduardoS

If you simple pick a C program and generate a .asm that makes the same operations the asm won't be fast, but if you pick a C program and rewrite it using asm it will probably be faster for the reasons listed here.
Your example have a big problem, the slowest part is the calls to write strings, and they are windows calls, equals for both programs, so the time of both will be similar.