Jump to content
  • Advertisement
Sign in to follow this  
Gink

Speed Question - ASM vs C

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!