|
-
Sep 12th, 2005, 07:28 AM
#1
Why is this ASM slower than C++?
PHP Code:
int inline MulAdd__asm(int val, int m, int a)
{
__asm
{
mov eax, val
imul eax, m
add eax, a
}
}
int MulAdd__cpp(int val, int m, int a)
{
return (val * m) + a;
}
Why is the inline ASM function about 3 times slower than the C++ version as shown above?
I don't live here any more.
-
Sep 12th, 2005, 11:23 AM
#2
Re: Why is this ASM slower than C++?
I'm not sure if this would do it, but add is mainly done with 16 bit registers/memory. Same holds true with mul. Try iadd eax, a.
Also isn't it suppose to be written like this:
inline int MulAdd__asm(int val, int m, int a)
Where the inline and int are swapped around?
-
Sep 12th, 2005, 11:25 AM
#3
Re: Why is this ASM slower than C++?
Woss, try it without inlining.
Jacob, iadd doesn't exist
-
Sep 12th, 2005, 11:27 AM
#4
Re: Why is this ASM slower than C++?
I thought it did? They got fadd, imul, fmul, and why not iadd? Weird.
-
Sep 12th, 2005, 12:37 PM
#5
Member
Re: Why is this ASM slower than C++?
What does the c++ code compile too?
Any chance you call the function with fixed values, so the multiply and addition are optimized away, making the function simply return a fixed number?
-
Sep 12th, 2005, 08:04 PM
#6
Re: Why is this ASM slower than C++?
 Originally Posted by penagate
Woss, try it without inlining.
Jacob, iadd doesn't exist 
Oh and just to let wossy and others know, it's always best to only inline when there are more than 5 lines of code in your function/sub. Otherwise it will execute slower than the actual call. The reason why it is like this, I have no idea. Maybe someone can enlighten me.
-
Sep 13th, 2005, 03:20 AM
#7
Re: Why is this ASM slower than C++?
I have tried it with and without the inline keyword and the performance is identical.
I don't claim to be any kind of ASM programmer but I was surprised by the outcome.
Thanks for the insight.
I don't live here any more.
-
Sep 13th, 2005, 07:31 AM
#8
Re: Why is this ASM slower than C++?
I've timed both and looked at the compiler listing and both functions compile to exactly the same code. So I don't understand why the inline ASM version is so much slower.
-
Sep 13th, 2005, 07:56 AM
#9
Re: Why is this ASM slower than C++?
Have you looked at the entire listing? The compiler can optimize across the C++ code, but it can't across the assembly.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 13th, 2005, 08:03 AM
#10
Re: Why is this ASM slower than C++?
Thanks CornedBee, it was indeed elsewhere in the listing... Being set up for speed the compiler had inlined the functions and it had optimised the C++ version.
Woss, you'll be pleased to know that turning off optimisations (forcing the compiler to make proper function calls) got me these results:
Code:
C++ code time: 782 ms
ASM code time: 765 ms
It was rather hasty and used GetTickCount but you can see the times are about the same, as you'd expect.
-
Sep 13th, 2005, 08:33 AM
#11
Re: Why is this ASM slower than C++?
Verrrrrry interrestink.
Am I right in thinking that any pentium-targetted ASM code can be used in C++? Or does VC++ have a preferred flavour? I'm still struggling to find any tutorials that don't require "a basic understanding of registers and machine language" or a PHD in computer science. I could do with a decent beginner's book really. I'm finding it totally innacessible right now
I don't live here any more.
-
Sep 13th, 2005, 08:38 AM
#12
Re: Why is this ASM slower than C++?
This is the PDF book I learnt basic assembly from - "Programming From The Ground Up" by Jonathon Bartlett. It's targeted for Linux, and syntax is a bit different from VC++'s inline assembly, but the basics are all the same. It teaches you about CPU and memory architecutre and starts right from the basics.
-
Sep 13th, 2005, 08:40 AM
#13
Re: Why is this ASM slower than C++?
How do you see the assembly code of a VC++.NET program? It somewhere in the menu. Can't seem to find it though. Maybe we can compare the assembly instructions with the ones in the function.
-
Sep 13th, 2005, 08:44 AM
#14
Re: Why is this ASM slower than C++?
Well, C++ only provides the asm keyword and states this:
7.4 The asm declaration [dcl.asm]
1 An asm declaration has the form
asm-definition:
asm ( string-literal ) ;
The meaning of an asm declaration is implementation-defined. [Note:
Typically it is used to pass information through the implementation to
an assembler. ]
As you see, the definition is minimal. It should also be mentioned that VC++ does not support this syntax, only the _asm {} syntax. GCC supports the syntax, but extends it for better interaction with the compiler.
What code you can use thus depends on your compiler or the underlying assembler. For example, in VC++ the compiler needs to understand the assembly, thus disabling use of SSEx in VC++6 at least.
GCC doesn't really understand any assembly, but it forwards it to the underlying assembler, and thus pretty much everything works, depending on the asembler.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 13th, 2005, 08:44 AM
#15
Re: Why is this ASM slower than C++?
 Originally Posted by Jacob Roman
How do you see the assembly code of a VC++.NET program? It somewhere in the menu. Can't seem to find it though. Maybe we can compare the assembly instructions with the ones in the function.
I did that. They are exactly the same. The difference came from the fact that the compiler (when optimised for speed) inlined all of them and (for me) somehow eliminated my benchmark loop for the C++ version.
To choose to see the ASM listing when you build an app go to Project -> xxx Properties -> C/C++ -> Output Files -> Assembler Output.
-
Sep 19th, 2005, 04:44 PM
#16
Re: Why is this ASM slower than C++?
Thanks penagate, I'll do that
-
Sep 27th, 2005, 06:46 PM
#17
Re: Why is this ASM slower than C++?
Woss, what's your ulitmate objective here? Are you simply trying to learn ASM, or are you trying to do custom functions to get a performance boost? I don't know if the latter is still worth the effort these days. There are so many considerations for actual execution speed, the casual optimizer will probably not do as well as the compiler optimizer.
My usual boring signature: Nothing
 
-
Sep 29th, 2005, 08:09 AM
#18
Re: Why is this ASM slower than C++?
Well I was just playing around with the code really, I have no objective yet. I took delivery of my ASM book yesterday so I can learn it properly.
I don't live here any more.
-
Oct 7th, 2005, 05:05 PM
#19
Re: Why is this ASM slower than C++?
Ah, world domination then. Ok.
My usual boring signature: Nothing
 
-
Oct 10th, 2005, 04:11 AM
#20
Re: Why is this ASM slower than C++?
Shhh don't tell everyone.
I don't live here any more.
-
Oct 11th, 2005, 02:23 AM
#21
Re: Why is this ASM slower than C++?
LOL.
Some of you people just crack me up..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|