| | |
compound operator proving efficiency
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
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
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
>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".
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.
>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?
>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.
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)
#include <ctime> #include <climits> #include <iostream> template <typename T> double runassignadd ( unsigned long n ) { T a = T(); T b = T(); std::clock_t start = std::clock(); for ( unsigned long i = 0; i < n; i++ ) a += b; return ( (double)std::clock() - start ) / CLOCKS_PER_SEC; } template <typename T> double runadd ( unsigned long n ) { T a = T(); T b = T(); std::clock_t start = std::clock(); for ( unsigned long i = 0; i < n; i++ ) a = a + b; return ( (double)std::clock() - start ) / CLOCKS_PER_SEC; } int main() { std::cout<<"A += B -- "<< runassignadd<int> ( UINT_MAX ) <<'\n'; std::cout<<"A = A + B -- "<< runadd<int> ( UINT_MAX ) <<'\n'; }
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.
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:
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
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)
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int a = 10; int b = 6; unsigned int i; int j; clock_t start_time, end_time; start_time = clock( ); for( j = 0; j < 10; j++ ) for( i = 0; i < 4000000000 ; i++) { a = 10; a = a + b; } end_time = clock( ); cout << "time: " << (double)(end_time - start_time)/ CLOCKS_PER_SEC << endl << endl; cout << start_time << '\t' << end_time << endl; return 0; }// end of main
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,108
Reputation:
Solved Threads: 169
> 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
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
>c++ -O3 -S -fomit-frame-pointer operator.cc
an extract of the relevant parts of the assembly generated (gcc 4.2.3):
> 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)
int func_int_plus( int a, int b ) { return a = a + b ; } int func_int_plus_assign( int a, int b ) { return a += b ; } struct A { A( int xx, int yy ) : x(xx), y(yy) {} A operator+ ( const A& that ) const { return A( x+that.x, y+that.y ) ; } A& operator+= ( const A& that ) { x += that.x ; y += that.y ; return *this ; } int x ; int y ; }; A func_A_plus( A a, A b ) { return a = a + b ; } A func_A_plus_assign( A a, A b ) { return a += b ; } A& func_A_plus_assign_byref( A& a, const A& b ) { return a += b ; }
>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)
.file "operator.cc" ///////////////////////////////// _Z13func_int_plusii: .LFB2: movl 8(%esp), %eax addl 4(%esp), %eax ret .LFE2: ////////////////////////////////// _Z20func_int_plus_assignii: .LFB3: movl 8(%esp), %eax addl 4(%esp), %eax ret .LFE3: ////////////////////////////////// _Z11func_A_plus1AS_: .LFB9: pushl %ebx .LCFI0: movl 8(%esp), %ebx movl 12(%esp), %ecx addl 16(%esp), %ebx addl 20(%esp), %ecx movl %ecx, %edx movl %ebx, %eax popl %ebx output ret ////////////////////////////////// _Z18func_A_plus_assign1AS_: .LFB10: pushl %ebx .LCFI1: movl 12(%esp), %ecx movl 8(%esp), %ebx addl 16(%esp), %ebx addl 20(%esp), %ecx movl %ecx, %edx movl %ebx, %eax popl %ebx ret .LFE10: ////////////////////////////////// _Z24func_A_plus_assign_byrefR1ARKS_: .LFB11: movl 4(%esp), %eax movl 8(%esp), %ecx movl (%ecx), %edx addl %edx, (%eax) movl 4(%ecx), %edx addl %edx, 4(%eax) ret .LFE11: //////////////////////////////////
![]() |
Other Threads in the C++ Forum
- Previous Thread: Which C++ complier to use
- Next Thread: Input operator overloading
Views: 1446 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework http iamthwee input int lazy linker list loop loops map math matrix member memory multidimensional network newbie number object objects opengl output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference server sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio win32 window windows winsock






