# Speed Question - ASM vs C

## Recommended Posts

Gink    100
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)
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 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 on other sites
ext    180
printf() is for formatted output so there is of course some overhead if you're only printing a simple line of text

##### Share on other sites
Oluseyi    2103
Quote:
 Original post by GinkIs 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 on other sites
Name_Unknown    100
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 on other sites
Gink    100
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 on other sites
Guest Anonymous Poster
indeed, puts("FF is now a string"); should be faster due to no formatting..

##### Share on other sites
Gink    100
puts took 30 seconds..

##### Share on other sites
zoggo    194
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 on other sites
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.

##### Share on other sites
Gink    100
Quote:
 Original post by Daniel Miller1. 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 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 on other sites
Gink    100
Quote:
 Original post by zoggoVisual 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 on other sites
zoggo    194
On my system...

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

(Please excuse slightly odd drive letter)

##### Share on other sites
Quote:
Original post by Gink
Quote:
 Original post by Daniel Miller1. 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 on other sites
Gink    100
#include <stdio.h>int main(){     char* s = "FF is now a String!";          for(int x=0; x < 0xFFFF;x++){          puts(s);     }}

##### 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 on other sites
JohnBSmall    881
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 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 on other sites
Read his responses and its clear he's trying to brag about the glorious speed of ASM.

##### Share on other sites
Gink    100
Quote:
 Original post by Daniel MillerRead 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 on other sites
Name_Unknown    100
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 on other sites
Gink    100
Quote:
 Original post by Name_UnknownWell 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 on other sites
Quote:
Original post by Gink
Quote:
 Original post by Name_UnknownWell 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]