compound operator proving efficiency

Thread Solved

Join Date: Feb 2008
Posts: 7
Reputation: johnRiley is an unknown quantity at this point 
Solved Threads: 0
johnRiley johnRiley is offline Offline
Newbie Poster

compound operator proving efficiency

 
0
  #1
Feb 5th, 2008
Hi all,

I've been searching for hours but i can't find any solutions so i hope someone can help...

Is there any way of proving that the += operator is more efficient than the + operator...

i.e. A += B instead of A = A + B

I'm hoping to use code to see how much overhead is being saved.


thanks in advance,

John Riley
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,311
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: compound operator proving efficiency

 
0
  #2
Feb 5th, 2008
>Is there any way of proving that the += operator is more efficient than the + operator...
Sure. Look at it and say "Hey, A is being evaluated twice with the + operator but only once with the += operator" and "Hey, A + B is probably making a temporary copy".
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: johnRiley is an unknown quantity at this point 
Solved Threads: 0
johnRiley johnRiley is offline Offline
Newbie Poster

Re: compound operator proving efficiency

 
0
  #3
Feb 5th, 2008
It is more efficient and i know that the standard operator assignment is being performed on a temporary value.

My question is, is there a way of proving it with code? Otherwise how do we know for sure.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,311
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: compound operator proving efficiency

 
0
  #4
Feb 5th, 2008
>My question is, is there a way of proving it with code?
Well, that's different from your original question, isn't it? Why not perform the operations a few billion times and see which one takes longer?
  1. #include <ctime>
  2. #include <climits>
  3. #include <iostream>
  4.  
  5. template <typename T>
  6. double runassignadd ( unsigned long n )
  7. {
  8. T a = T();
  9. T b = T();
  10. std::clock_t start = std::clock();
  11.  
  12. for ( unsigned long i = 0; i < n; i++ )
  13. a += b;
  14.  
  15. return ( (double)std::clock() - start ) / CLOCKS_PER_SEC;
  16. }
  17.  
  18. template <typename T>
  19. double runadd ( unsigned long n )
  20. {
  21. T a = T();
  22. T b = T();
  23. std::clock_t start = std::clock();
  24.  
  25. for ( unsigned long i = 0; i < n; i++ )
  26. a = a + b;
  27.  
  28. return ( (double)std::clock() - start ) / CLOCKS_PER_SEC;
  29. }
  30.  
  31. int main()
  32. {
  33. std::cout<<"A += B -- "<< runassignadd<int> ( UINT_MAX ) <<'\n';
  34. std::cout<<"A = A + B -- "<< runadd<int> ( UINT_MAX ) <<'\n';
  35. }
>Otherwise how do we know for sure.
Don't put too much weight on empirical tests for making a general statement. There are often unknowns that skew the result and depend on the machine you test with, the code you use, and so on.
Last edited by Narue; Feb 5th, 2008 at 3:41 pm.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,820
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 213
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: compound operator proving efficiency

 
0
  #5
Feb 5th, 2008
Short of examining the object code created, probably no definitive answer. It may be compiler dependent.

In a brute force approach, run the two operations in a verrrrry big loop, time the differences. I just tried this:
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int a = 10;
  10. int b = 6;
  11. unsigned int i;
  12. int j;
  13.  
  14. clock_t start_time, end_time;
  15.  
  16. start_time = clock( );
  17.  
  18. for( j = 0; j < 10; j++ )
  19. for( i = 0; i < 4000000000 ; i++)
  20. {
  21. a = 10;
  22. a = a + b;
  23. }
  24.  
  25. end_time = clock( );
  26. cout << "time: "
  27. << (double)(end_time - start_time)/ CLOCKS_PER_SEC << endl << endl;
  28.  
  29. cout << start_time << '\t' << end_time << endl;
  30.  
  31. return 0;
  32. }// end of main
changing the a = a + b; to the += version in a second test. Running on a 3.2GHz Pentium D, the timings were:
a = a + b 109.83
a += b 109.143

Hmmm, is there any significant, practical difference? I think not. Remember, that was 40 billion iterations.

YMMV
Val
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: johnRiley is an unknown quantity at this point 
Solved Threads: 0
johnRiley johnRiley is offline Offline
Newbie Poster

Re: compound operator proving efficiency

 
0
  #6
Feb 6th, 2008
I do like the idea of timing the different assignments. What about actually changing or overloading the += operator? any ideas or suggestions?

does anyone know which header file holds the += operator code?

My apologies to Narue... my original question was different but i meant no offense.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,108
Reputation: vijayan121 is a splendid one to behold vijayan121 is a splendid one to behold vijayan121 is a splendid one to behold vijayan121 is a splendid one to behold vijayan121 is a splendid one to behold vijayan121 is a splendid one to behold 
Solved Threads: 169
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: compound operator proving efficiency

 
0
  #7
Feb 6th, 2008
> Hmmm, is there any significant, practical difference?
> I think not. Remember, that was 40 billion iterations.

for standard types like int or double, it would not make any difference. the compiler knows everything about these types and can see that
a = a + b ; and a += b ; are equivalent and generate identical (optimized) code.

the case could be different for user defined types; for one the overloaded + or += operators may not be inline and the compiler cannot make any assumptions about the equivalence between
a = a + b ; and a += b ; . if the operatos are inline, a good compiler can still optimize; even in this case, avoiding the creation of anonymous temporaries can improve performance.

  1. int func_int_plus( int a, int b )
  2. {
  3. return a = a + b ;
  4. }
  5.  
  6. int func_int_plus_assign( int a, int b )
  7. {
  8. return a += b ;
  9. }
  10.  
  11. struct A
  12. {
  13. A( int xx, int yy ) : x(xx), y(yy) {}
  14. A operator+ ( const A& that ) const
  15. { return A( x+that.x, y+that.y ) ; }
  16. A& operator+= ( const A& that )
  17. { x += that.x ; y += that.y ; return *this ; }
  18. int x ;
  19. int y ;
  20. };
  21.  
  22. A func_A_plus( A a, A b )
  23. {
  24. return a = a + b ;
  25. }
  26.  
  27. A func_A_plus_assign( A a, A b )
  28. {
  29. return a += b ;
  30. }
  31.  
  32. A& func_A_plus_assign_byref( A& a, const A& b )
  33. {
  34. return a += b ;
  35. }

>c++ -O3 -S -fomit-frame-pointer operator.cc
an extract of the relevant parts of the assembly generated (gcc 4.2.3):
  1. .file "operator.cc"
  2. /////////////////////////////////
  3. _Z13func_int_plusii:
  4. .LFB2:
  5. movl 8(%esp), %eax
  6. addl 4(%esp), %eax
  7. ret
  8. .LFE2:
  9. //////////////////////////////////
  10. _Z20func_int_plus_assignii:
  11. .LFB3:
  12. movl 8(%esp), %eax
  13. addl 4(%esp), %eax
  14. ret
  15. .LFE3:
  16. //////////////////////////////////
  17. _Z11func_A_plus1AS_:
  18. .LFB9:
  19. pushl %ebx
  20. .LCFI0:
  21. movl 8(%esp), %ebx
  22. movl 12(%esp), %ecx
  23. addl 16(%esp), %ebx
  24. addl 20(%esp), %ecx
  25. movl %ecx, %edx
  26. movl %ebx, %eax
  27. popl %ebx
  28. output ret
  29. //////////////////////////////////
  30. _Z18func_A_plus_assign1AS_:
  31. .LFB10:
  32. pushl %ebx
  33. .LCFI1:
  34. movl 12(%esp), %ecx
  35. movl 8(%esp), %ebx
  36. addl 16(%esp), %ebx
  37. addl 20(%esp), %ecx
  38. movl %ecx, %edx
  39. movl %ebx, %eax
  40. popl %ebx
  41. ret
  42. .LFE10:
  43. //////////////////////////////////
  44. _Z24func_A_plus_assign_byrefR1ARKS_:
  45. .LFB11:
  46. movl 4(%esp), %eax
  47. movl 8(%esp), %ecx
  48. movl (%ecx), %edx
  49. addl %edx, (%eax)
  50. movl 4(%ecx), %edx
  51. addl %edx, 4(%eax)
  52. ret
  53. .LFE11:
  54. //////////////////////////////////
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1446 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC