Assembly vs. C++ Performance

Reply

Join Date: Nov 2008
Posts: 2
Reputation: cpsusie is an unknown quantity at this point 
Solved Threads: 0
cpsusie cpsusie is offline Offline
Newbie Poster

Assembly vs. C++ Performance

 
0
  #1
Nov 3rd, 2008
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:

  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:

  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:

  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:

  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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: Moogly is an unknown quantity at this point 
Solved Threads: 0
Moogly Moogly is offline Offline
Newbie Poster

Re: Assembly vs. C++ Performance

 
0
  #2
Nov 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,516
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 189
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Assembly vs. C++ Performance

 
0
  #3
Nov 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: bionicseraph is an unknown quantity at this point 
Solved Threads: 6
bionicseraph bionicseraph is offline Offline
Newbie Poster

Re: Assembly vs. C++ Performance

 
0
  #4
Nov 12th, 2008
Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,516
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 189
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Assembly vs. C++ Performance

 
0
  #5
Nov 12th, 2008
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....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: cpsusie is an unknown quantity at this point 
Solved Threads: 0
cpsusie cpsusie is offline Offline
Newbie Poster

Re: Assembly vs. C++ Performance

 
0
  #6
Nov 17th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC