943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1878
  • C++ RSS
Feb 5th, 2008
0

compound operator proving efficiency

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnRiley is offline Offline
7 posts
since Feb 2008
Feb 5th, 2008
0

Re: compound operator proving efficiency

>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".
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 5th, 2008
0

Re: compound operator proving efficiency

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnRiley is offline Offline
7 posts
since Feb 2008
Feb 5th, 2008
0

Re: compound operator proving efficiency

>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?
C++ Syntax (Toggle Plain Text)
  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 4:41 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 5th, 2008
0

Re: compound operator proving efficiency

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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 6th, 2008
0

Re: compound operator proving efficiency

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnRiley is offline Offline
7 posts
since Feb 2008
Feb 6th, 2008
0

Re: compound operator proving efficiency

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

c++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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. //////////////////////////////////
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Which C++ complier to use
Next Thread in C++ Forum Timeline: Input operator overloading





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


Follow us on Twitter


© 2011 DaniWeb® LLC