Sign in to follow this  
Gink

Speed Question - ASM vs C

Recommended Posts

As a simple test, I wanted to see how much faster ASM was than C, so I decided to compare printf to Display String function 9. I was surprised to see that ASM displayed 0xFFFF strings in 3 seconds compared to the 21 seconds it took in C. Is this kind of speed difference normal? Also can C ever be as fast as ASM when it comes to displaying strings? Its written as real mode flat model. Is my ASM code wrong or is there a huge speed difference between C and ASM? edit : Also tried creating the string before printf - same result Compiled with GCC Here is the code

#include <stdio.h>

int main(){
     for(register int x=0; x < 0xFFFF; x++){    
           printf("FF is now a string\n");
     }
}


[BITS 16]
[ORG 0x100]

%include "Byte2Str.MAC"

[SECTION .text]

mov AL,0xFF  ;load number into AL (ByteToString uses AL)
mov SI,msg   ;Load address of string
ByteToString ;converts a 1 byte number into a 2 byte string

mov DX,msg
mov AX,0x0900  ;Function 9 - printstring

mov CX,0xFFFF ;Loop 0xFFFF Times
looptop:
dec CX
INT 0x21       ;DOS Dispatcher
jnz looptop

mov AX,0x4C00  ;0x4C00 - Terminate Program
int 0x21       ;Dos Dispatcher


[SECTION .data]
Digits db '0123456789ABCDEF'
msg db "   is now a string!",13,10,'$'

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The speed of implementation in C outweights the difference in runtime comparison. The reason for your loss of speed in C is probably because you have to call into the standard library, which might be dynamically linked on top if it all.
Real speed differences occur when you compare a good solution optimised for a specific problem to a lousy solution optimised for nothing or a general case of the problem.
It is only worth using assembly code in kernel/driver development and perhaps as a last line of defence in a rendering pipeline. (Don't bother flaming me for that opinion)

Share this post


Link to post
Share on other sites
printf() is for formatted output so there is of course some overhead if you're only printing a simple line of text

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
Is this kind of speed difference normal?
Is this kind of speed difference meaningful? Does printf do the exact same thing as the ASM display routine, or is there a greater overhead because printf provides greater functionality?

You're not asking informed - and, therefore, useful - questions. Go take a look at the source to printf to determine what it does, then extract the portion that just displays a string - the part that does exactly the same thing as ASM - and only then can you begin to make useful benchmarks.

Share this post


Link to post
Share on other sites
Yeah sure it was oldhat in DOS days to do that. If you don't need portability and will only run on DOS do it and have fun, there are a million tricks you could do in DOS you can't do on modern OS anymore. But it's kind of useless these days...

Share this post


Link to post
Share on other sites
So is there any function in C that can write to output as fast as the one ASM has? Also where can i find the Source for printf

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
indeed, puts("FF is now a string"); should be faster due to no formatting..

Share this post


Link to post
Share on other sites
Visual C++ ships with Microsofts implementation of printf. GNU's C implementation is Open Source and thus by definition, the source is available.

Before you post any more meaningless results, I suggest you try to figure out what is the difference between the two code paths.

You can use DEBUG.COM to trace through both your assembly code and the interupt, (yea, I know you shouldn't trace through interupts but it is a software one) and most C dev environments allow you to single step through both your C code and the machine code it generated.

Share this post


Link to post
Share on other sites
Quote:
Original post by Daniel Miller
1. Go inside of printf and remove all of the of the formatting code, etc., and do a test run with that.

2. Ask yourself why this all matters.



25 seconds

Share this post


Link to post
Share on other sites
Here are my 2c. First of all, you can't compare C with ASM. You'll need to compare between a specific C compilere such as CL or GCC and an assembly program.

Second, your test wasn't "fair", as you have used printf instead of puts. Even so, puts may be more generic than your assembly implementation.

And thirdly, in THEORY you could optimize your C code by turning it into a handcrafted assembly piece. However, this will usually fail, as compilers today are quite good with optimizations, and in order to get to the same exact results with better timings you will have to get familiar with deep CPU architecture, things like loops unwinding (which is also an optimization for C code), cache lines and you will have to thoroughly know assembly.

As a rule of thumb - the first thing to do is to code your code AS SIMPLE AS POSSIBLE - The KISS guideline should be on top of everything else (KISS is "Keep it simple and stupid" or "Keep it simple, stupid!", depends on how you look at it :-)). When your application is complete, running and working with no apparent bugs - check your bottlenecks and decide what to do with them. Often you will do much better by checking your algorithms and optimizing them, than to go for Assembly.

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by zoggo
Visual C++ ships with Microsofts implementation of printf. GNU's C implementation is Open Source and thus by definition, the source is available.

Before you post any more meaningless results, I suggest you try to figure out what is the difference between the two code paths.

You can use DEBUG.COM to trace through both your assembly code and the interupt, (yea, I know you shouldn't trace through interupts but it is a software one) and most C dev environments allow you to single step through both your C code and the machine code it generated.



Where is this source? (Dev-CPP) I tried a search and couldnt find it

Btw, just because C cant go toe to toe with ASM doesnt mean you have to get all defensive.

Share this post


Link to post
Share on other sites
On my system...

D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\crt\src\printf.c

(Please excuse slightly odd drive letter)

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
Quote:
Original post by Daniel Miller
1. Go inside of printf and remove all of the of the formatting code, etc., and do a test run with that.

2. Ask yourself why this all matters.



25 seconds


I don't believe that. Post your code. Also, why does it matter whether a printing function is faster in one archiac language than in another (archaic language [grin])?

Share this post


Link to post
Share on other sites


#include <stdio.h>

int main(){
char* s = "FF is now a String!";

for(int x=0; x < 0xFFFF;x++){
puts(s);
}
}


Share this post


Link to post
Share on other sites
No, go inside of printf and remove the superflous code. It's like running a graphics test where on one maching you render wirefame 3D scene, and on another you render that same 3D scene but fully textured. You aren't comparing apples to apples.

And for the third time, why does the speed of the print function matter!?

Share this post


Link to post
Share on other sites
For all those who are talking about how printf provides more functionality and you should use puts, this is only partially correct. I remember at one point when I was messing around with assembly myself, I looked at some of the assembly output from something along the lines of:

int main(int argc, char *argv[])
{
printf("Hello, world!\n");
return 0;
}


I was surprised (but pleased) to discover that the compiler actually knew enough to optimize the printf call by turning it into a call to puts. Apparently, with the compiler and settings I was using, the compiler actually checked the format string and was able to turn it into a puts() because it didn't contain any actual format codes.

Compilers are sometimes smarter than you think (and sometimes the opposite is true)

John B

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Everyone, don't be so harsh...He/she originally asked:

Quote:
As a simple test, I wanted to see how much faster ASM was than C, so I decided to compare printf to Display String function 9


The main question is "How much faster is ASM than C?" Lets try to focus on that instead of the poor decision of using printf to compare the speeds.

ASM 'can' be written to be much faster than C, although most modern compilers are getting VERY good at optimization. The problem with ASM is it takes a very long time to write anything good. For this reason, you must analyze your code and decide where the best use of ASM would be (if any).

Share this post


Link to post
Share on other sites
Quote:
Original post by Daniel Miller
Read his responses and its clear he's trying to brag about the glorious speed of ASM.


No im not, its a well known fact that ASM can be faster than a HLL, there is no need for me to brag about ASM being faster. I was just wondering if compilers will ever get to the point where they will be just as fast as ASM. But it seems ive offended some people by comparing printstring function to printf and puts

Share this post


Link to post
Share on other sites
Well the problem is he is making a rather meaningless comparison...

he compares a heavy duty standard library call to a 10 line asm function using DOS I/O calls..

it is well known printf() is slow but it wasn't meant for printing 65536 strings in a loop for a kind of pointless speed test...

your comparison is flawed because nobody in their right mind uses console I/O like this... but yeah calling the DOS I/O was faster and why alot of old DOS programs used it because on an 8086 4 mhz machine you need all the speed you can get. On a 3ghz+ processor on Windows printf is fast enough for normal use...

Share this post


Link to post
Share on other sites
Quote:
Original post by Name_Unknown
Well the problem is he is making a rather meaningless comparison...

he compares a heavy duty standard library call to a 10 line asm function using DOS I/O calls..

it is well known printf() is slow but it wasn't meant for printing 65536 strings in a loop for a kind of pointless speed test...

your comparison is flawed because nobody in their right mind uses console I/O like this... but yeah calling the DOS I/O was faster and why alot of old DOS programs used it because on an 8086 4 mhz machine you need all the speed you can get. On a 3ghz+ processor on Windows printf is fast enough for normal use...

Sure no one is gonna read those 65k lines. But im just wondering why the compiler isnt optimized to be just as fast as ASM for something like displaying a string.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
Quote:
Original post by Name_Unknown
Well the problem is he is making a rather meaningless comparison...

he compares a heavy duty standard library call to a 10 line asm function using DOS I/O calls..

it is well known printf() is slow but it wasn't meant for printing 65536 strings in a loop for a kind of pointless speed test...

your comparison is flawed because nobody in their right mind uses console I/O like this... but yeah calling the DOS I/O was faster and why alot of old DOS programs used it because on an 8086 4 mhz machine you need all the speed you can get. On a 3ghz+ processor on Windows printf is fast enough for normal use...

Sure no one is gonna read those 65k lines. But im just wondering why the compiler isnt optimized to be just as fast as ASM for something like displaying a string.


Because it doesn't matter. [smile]

Share this post


Link to post
Share on other sites
The original post makes absolutely no sense at all. The assembly program is a 16 bit real mode snippet to print out a string via interrupts. The C program is a 32 bit protected mode program that makes a function call to a formatted print function that will request the OS to write to a console window. You didn't even tell us what kind of optimizations the C program is compiled with.

In other words, your comparison borders on retarded.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this