943,891 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 3rd, 2008
0

Assembly vs. C++ Performance

Expand Post »
Alright guys, I'm a total n00b to assembly. NOTE: I am not a student -- I am a professional lawyer who loves to tinker with the computer and waste time.

Using Visual Studio 2008 Express (C++), I actually managed to write a function in assembly language that evaluates a character, and if it is a lower case letter, returns the corresponding uppercase letter, if not a lower case letter, returns the same character evaluated.

I wrote this function in C++, assembly (in-line) and assembly (MASM).

Here is the C++ Code:

C++ Syntax (Toggle Plain Text)
  1.  
  2. char cppToUpper(char c)
  3. {
  4. if (c > 122 || c < 97 )
  5. return c;
  6. else return c - 32;
  7. }

Here is the inline assembly Code:

C++ Syntax (Toggle Plain Text)
  1.  
  2. char cToUpper(int c)
  3. {
  4. //
  5. //cout << cLowerLimit;
  6. _asm
  7. {
  8.  
  9. //Copy the character onto the arithmetic register for single bytes
  10. mov eax, c;
  11.  
  12. //Test the Upper Limit
  13. cmp eax, 122; // Compare the Character to 122
  14. ja End; // Jump to the end if above -- the character is too high to be a lower case letter
  15.  
  16. //Test the lower limit
  17. cmp eax, 97 //Compare the character to 97
  18. jb End; // Jump to the end if below == the character is too low to be a lower case letter
  19.  
  20. //Now the operation begins
  21. sub eax, 32; //Subtract 32 from the character in the register
  22.  
  23.  
  24. End:
  25. // mov result, al; //Move the Character in the register into the result variable
  26.  
  27. }
  28.  
  29.  
  30. }

And here is the function in pure assembly language:

Assembly Syntax (Toggle Plain Text)
  1.  
  2. .686
  3. .model flat, stdcall
  4. option casemap :none
  5.  
  6. .code
  7.  
  8. cUpperCase2 proc cValue:DWORD
  9.  
  10. mov eax, cValue
  11.  
  12. cmp eax, 122
  13.  
  14. ja TEnd
  15.  
  16. cmp eax, 97
  17.  
  18. jb TEnd
  19.  
  20. sub eax, 32
  21.  
  22.  
  23.  
  24. TEnd:
  25.  
  26. ret
  27.  
  28. cUpperCase2 endp
  29.  
  30. end

Now, here is what the C++ function disassembles to:

Assembly Syntax (Toggle Plain Text)
  1.  
  2. char cppToUpper(char c)
  3. {
  4. 01271680 push ebp
  5. 01271681 mov ebp,esp
  6. 01271683 sub esp,0C0h
  7. 01271689 push ebx
  8. 0127168A push esi
  9. 0127168B push edi
  10. 0127168C lea edi,[ebp-0C0h]
  11. 01271692 mov ecx,30h
  12. 01271697 mov eax,0CCCCCCCCh
  13. 0127169C rep stos dword ptr es:[edi]
  14. if (c > 122 || c < 97 )
  15. 0127169E movsx eax,byte ptr [c]
  16. 012716A2 cmp eax,7Ah
  17. 012716A5 jg cppToUpper+30h (12716B0h)
  18. 012716A7 movsx eax,byte ptr [c]
  19. 012716AB cmp eax,61h
  20. 012716AE jge cppToUpper+37h (12716B7h)
  21. return c;
  22. 012716B0 mov al,byte ptr [c]
  23. 012716B3 jmp cppToUpper+3Eh (12716BEh)
  24. 012716B5 jmp cppToUpper+3Eh (12716BEh)
  25. else return c - 32;
  26. 012716B7 movsx eax,byte ptr [c]
  27. 012716BB sub eax,20h
  28. }
  29. 012716BE pop edi
  30. 012716BF pop esi
  31. 012716C0 pop ebx
  32. 012716C1 mov esp,ebp
  33. 012716C3 pop ebp
  34. 012716C4 ret

ALRIGHT HERE'S the question. Why is the C++ code considerably faster even though it compiles to far more instructions than my assembly language code uses? 48 "ticks" expire when executing the pure assembly language function 10,000,000 times (I'll put this stuff at the very bottom); 0 ticks when executing it in C++, and 16 when using inline assembly?

I am impressed that I was even able to get it to work in assembly but perplexed at the performance results. I'll put the main() function below along with the efficiency timing stuff for your reference.

Any ideas? I am just trying to learn a little assembly because I am curious about how computers actually work.

Thanks,
Chris

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include "windows.h"
  6. #include "time.h"
  7. using namespace std;
  8. extern "C" int _stdcall cUpperCase2(char c);
  9.  
  10. class stopwatch
  11. {
  12. public:
  13. stopwatch() : start(clock()){} //start counting time
  14. ~stopwatch();
  15. private:
  16. clock_t start;
  17. };
  18.  
  19. stopwatch::~stopwatch()
  20. {
  21. clock_t total = clock()-start; //get elapsed time
  22. cout<<"total of ticks for this activity: "<<total<<endl;
  23. cout <<"in seconds: "<< double(total/CLK_TCK) <<endl;
  24. }
  25.  
  26. void main()
  27. {
  28. bool bAgain = true;
  29.  
  30. while (bAgain)
  31. {
  32. // unsigned long lTimeNow = t_time;
  33.  
  34.  
  35.  
  36. char c = 'a';
  37. char d = '!';
  38. char e;
  39.  
  40. //cout << "A lowercase character will be converted to Uppercase:" << endl;
  41. //cin >> c;
  42. {
  43. stopwatch watch;
  44. for (int i=0; i < 10000000; i++)
  45. {
  46. e= cUpperCase2(c);
  47. e= cUpperCase2(d);
  48. }
  49.  
  50. }
  51. cout << "That was the external function written in assembler." << endl;
  52.  
  53. {
  54. stopwatch watch;
  55. for (int i=0; i < 10000000; i++)
  56. {
  57.  
  58. // cout << cppToUpper(c);
  59. //cout << cppToUpper(d);
  60. e= cppToUpper(c);
  61. e= cppToUpper(d);
  62. }
  63.  
  64. }
  65. cout << "That was C++\n";
  66. {
  67. stopwatch watch;
  68. for (int i=0; i < 10000000; i++)
  69. {
  70. e= cToUpper(c);
  71. e= cToUpper(d);
  72. }
  73.  
  74. }
  75. cout << "That was in line assembler\n";
  76.  
  77.  
  78.  
  79. cout << "Enter a letter and hit enter to exit (a will repeat) . . ." << endl;
  80. cin >> c;
  81.  
  82. //return 0;
  83. if (c != 'a')
  84. bAgain = false;
  85.  
  86. }
  87.  
  88. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpsusie is offline Offline
2 posts
since Nov 2008
Nov 11th, 2008
0

Re: Assembly vs. C++ Performance

Alright to simply answer your question as to why C++ is "faster" than Assembly, the reason being is because at some time in the 50's or 60's (don't remember when) at the Bell company (or some company) some programmers were sick of coding in Assembly Languages so they asked their bosses if they could create a simpler new language also known as C which was created in Assembly (correct me if I'm wrong considering thats what they were coding in.) at some point C++ was made (duno by who) which is an extended version of C (adding more capabilities) so C / C++ were made to make programming quicker because Assembly is a low level programming language (non-english syntax) and C / C++ along with a lot of other languages are high level languages (english-like syntax) so programming is easier because its more english like so it would take you less time to make a server for a huge game in C++ than it would in Assembly if you don't end up with a brain tumor before you finish.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Moogly is offline Offline
1 posts
since Nov 2008
Nov 11th, 2008
0

Re: Assembly vs. C++ Performance

The person who posted above me is completely wrong. Assembly is faster than any other code if you use/code it correctly. It could be that the optimizations the compiler made for the C++ code were good optimizations, whereas the code you wrote was poorly designed and written.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 12th, 2008
0

Re: Assembly vs. C++ Performance

The person who posted above me is completely wrong. Assembly is faster than any other code if you use/code it correctly. It could be that the optimizations the compiler made for the C++ code were good optimizations, whereas the code you wrote was poorly designed and written.
Best Jew: you are half right. Assembly speed = C++ as C++ is compiled into assembly code. With handwritten assembly, you might be able to make the routine more efficient than the C++ compiler does. On most modern systems though, the results aren't noticeable. The difference is that C++ is much faster development time while assembly is much better for custom processors.
Reputation Points: 10
Solved Threads: 6
Newbie Poster
bionicseraph is offline Offline
19 posts
since Nov 2008
Nov 12th, 2008
0

Re: Assembly vs. C++ Performance

Yeah, I know that. What I was saying is that if the C++ code was good to begin with, and the compiler optimized it well, whereas the assembly was poorly written....
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 17th, 2008
0

Re: Assembly vs. C++ Performance

Hi guys. Thanks for the replies. I kinda guessed that the reason why the routine in assembly was slower was because it was poorly written -- I am not a pro programmer and that seems like the only logical explanation. Nevertheless, I was rather surprised at that result -- compare the how many lines of code my assembly function takes to the lines of (assembly) code the c++ functionis compiled into. I was wondering whether you had any thoughts on why, in this particular example, the c++ code runs faster even though it has so many more lines of code. I was puzzled.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpsusie is offline Offline
2 posts
since Nov 2008
Jan 22nd, 2010
-1

The compiler

That code cant be best possible because assembly is much faster and u can know it because its used on bootloaders and other things that need extreme speed. i also know that microsoft compilers arent usually very good. For example microsoft visual c++ compiles code that does nothing into 155kb as mingw based (mingw is made from gcc) dev-cpp compiles it into 15kb. somebody should try some different compiler(NASM for example)
Reputation Points: 6
Solved Threads: 0
Newbie Poster
ljlassi is offline Offline
1 posts
since Jan 2010
Jan 22nd, 2010
0
Re: Assembly vs. C++ Performance
Measuring execution time is a very complex task...just check the link titled

[pdf]Measuring Program Execution Time


http://www.google.ca/#hl=en&source=h...102d89cbdc458f
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is online now Online
2,197 posts
since Jan 2008
Jan 25th, 2010
0
Re: Assembly vs. C++ Performance
hi cpsusi,

I can't find anything wrong with your assembly. The C++-output however looks awful. And I also don't have a clue why it perform worse than your C++-code.

A reason might be, that MASM puts tons of unnecessary stuff to your simple code. Try NASM instead. That really is a reliable and fast assembler. Maybe you can get the programm run faster than compiled in C++.
Reputation Points: 56
Solved Threads: 29
Posting Whiz in Training
sDJh is offline Offline
255 posts
since Aug 2005
Jan 29th, 2010
0
Re: Assembly vs. C++ Performance
Assembly code is the faster and lightweight(If written efficiently).

People say that C++ code is faster because in most cases and when the program is large, the code optimizer and intermediate code generator of C++ compiler produces more efficient assembly code than those written by hand.

If you write Assembly code properly, it will be faster

Please correct me if i am wrong
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vivek4020 is offline Offline
18 posts
since Nov 2009
Message:
Previous Thread in Assembly Forum Timeline: Access USB memory using Assembly
Next Thread in Assembly Forum Timeline: How to time-stamp an event in Windows?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC