943,692 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 78315
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Jul 12th, 2007
0

Re: C++ Performance Tips

Using an example will help.
For instance let a = 6 and b = 13.

a = a + b
Now a = 19 and b is unchanged at 13.

b - a - b or b = 19 - 13
Now a = 19 (unchanged) and b = 6.

a = a - b or a = 19 - 6
Now a = 13 and b = 6. The values are swapped.

For pure functionality, I don't recommend this method because it only works with summable values. Swapping using a temporary variable works in all cases.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kmillen is offline Offline
6 posts
since Apr 2007
Jul 12th, 2007
0

Re: C++ Performance Tips

>I don't recommend this method because it only works with summable values.
Not to mention that you also risk integer overflow and undefined behavior.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 6th, 2007
0

Re: C++ Performance Tips

Click to Expand / Collapse  Quote originally posted by yumvpvip ...
I don't think so. the result will be the fllowigs:
a=b;
b=0;
I am a beginner please give me some advice
actually it does work

assume a = 3 and b = 7

a = a + b

a = 10
b = 7

b = a - b
a = 10
b = 3

a = a - b
a = 7
b = 3

however from the responses this doesnt seem to be a very safe way of handling things.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jan 8th, 2008
0

Re: C++ Performance Tips

The most efficient way for swapping whole numbers is

int a = 10;
int b = 15;

a ^= b;
b ^= a;
a ^= b;

y because xor is an assembly level instruction and its much faster than any move operation.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Rajith Cherian is offline Offline
22 posts
since Dec 2007
Jan 8th, 2008
0

Re: C++ Performance Tips

>The most efficient way for swapping whole numbers is <snip xor swap>
>because xor is an assembly level instruction and its much faster than any move operation.
An empirical test disproves your claim. The swap using a temporary is noticeably faster on at least one compiler (Microsoft C++) given any optimization level.
C++ Syntax (Toggle Plain Text)
  1. #include <climits>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. void swap_temp ( int& a, int& b )
  6. {
  7. int temp = a;
  8. a = b;
  9. b = temp;
  10. }
  11.  
  12. void swap_xor ( int& a, int& b )
  13. {
  14. a ^= b;
  15. b ^= a;
  16. a ^= b;
  17. }
  18.  
  19. int main()
  20. {
  21. int a1 = 10, a2 = 20;
  22. int b1 = 10, b2 = 20;
  23. std::clock_t start;
  24.  
  25. std::cout<< a1 <<"\t"<< a2 <<'\n';
  26. std::cout<< b1 <<"\t"<< b2 <<'\n';
  27.  
  28. swap_temp ( a1, a2 );
  29. swap_temp ( b1, b2 );
  30.  
  31. std::cout<< a1 <<"\t"<< a2 <<'\n';
  32. std::cout<< b1 <<"\t"<< b2 <<'\n';
  33.  
  34. std::cout<<"swap_temp: ";
  35. start = std::clock();
  36. for ( unsigned i = 0; i < UINT_MAX; i++ )
  37. swap_temp ( a1, a2 );
  38. std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';
  39.  
  40. std::cout<<"swap_xor: ";
  41. start = std::clock();
  42. for ( unsigned i = 0; i < UINT_MAX; i++ )
  43. swap_temp ( b1, b2 );
  44. std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';
  45. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 28th, 2008
0

Re: C++ Performance Tips

This sticky topic is a very interesting read but I think most of the articles are dwelling very low level C++ stuff which is not all wrong but in the current modern age and days and unless you are into compiler or device driver or OS kernel level programming, such esoteric constructs do not really apply to business application level programming.

Why was STL declared as part of the C++ standard? Simply becuz enough wasted years are spent on re-inventing code like vector, list, map, common algorithms starting from scratch. By mandating STL as part of C++ standard, it make us C++ programmer very fast up and running and devote more time to focus on our own company business domain logic!!!!

With ACE, I am now looking to deploy a portable multi-threaded server program in weeks instead of months or even years previously. I hope soon ACE will be part of C++ standard too. Java success is becuz Java Collections, Multi-thread, Logging, Socket, Generics, ImageIO,NIO etc etc are PART OF THE SDK. It make Java programmers more efficient in producing application level code as compared to traditional C++.

Back to performance tip. My working experience with a mission critical system using BEA Tuxedo is software design and IO are the two main culprits of low performance. A properly designed and efficient choice of data structures and algorithms of the module will yield magnitudes more performance than tweaking the C++ loop constructs, bitwise operations etc etc etc.

IO is the other killer. If possible structure your program such that just the correct number of times we need to do IO is enough. Additional IO accesses should be forbidden (if possible!!!). Despite commercial database boast of their performance, my working experience is doing the correct data structures and algorithms in programs will already give me the much higher performance I need. The database performance will be the icing on the cake though.

Lastly, I hope Java Hibernate and Swing concept can be part of C++ standard too. It is time C++ standard catch up with Java in the database access area and GUI. C++ still suffer from the syndrome of being too low level and not abstracted high enough for fast application level programming and uses.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
sohguanh is offline Offline
16 posts
since Feb 2008
Feb 28th, 2008
0

Re: C++ Performance Tips

>C++ still suffer from the syndrome of being too low level and not
>abstracted high enough for fast application level programming and uses.
You're missing the point, I think. C++ and Java aren't competing for the same market. Think of it this way: Java is good for high level applications. C++ is good for the frameworks (OS, JVM, compilers, etc...) that Java needs to exist. Don't forget that even though it can be used for applications, C++ is and always has been a systems programming language.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 28th, 2008
0

Re: C++ Performance Tips

Great Job Guys !! Thanks.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
patelmiteshb is offline Offline
10 posts
since Mar 2008
May 5th, 2008
0

Re: C++ Performance Tips

Wow! I learned a lot of things from this thread! ... Thanks for the info everyone!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AyaToujo is offline Offline
1 posts
since May 2008
Jun 20th, 2008
-2

Re: C++ Performance Tips

Hello,
Thanks to share the tips of c++,you have taken these tips from the book Efficient c++,I think, Thinking in c++ is also a good book for c++,To Swap two variables, you use simple logic:
a=a+b;
b=a-b;
a=a-b;
Last edited by Cait; Jun 20th, 2008 at 3:39 am.
Reputation Points: 7
Solved Threads: 1
Newbie Poster
Cait is offline Offline
3 posts
since Jun 2008

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: Help with an overloaded operator
Next Thread in C++ Forum Timeline: Combined accessor/mutator?





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


Follow us on Twitter


© 2011 DaniWeb® LLC