RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 42532 | Replies: 42
Reply
Join Date: Apr 2007
Posts: 6
Reputation: kmillen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kmillen kmillen is offline Offline
Newbie Poster

Re: C++ Performance Tips

  #31  
Jul 12th, 2007
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ Performance Tips

  #32  
Jul 12th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 744
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: C++ Performance Tips

  #33  
Sep 6th, 2007
Originally Posted by yumvpvip View Post
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.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Join Date: Dec 2007
Location: Chennai
Posts: 22
Reputation: Rajith Cherian is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Rajith Cherian's Avatar
Rajith Cherian Rajith Cherian is offline Offline
Newbie Poster

Solution Re: C++ Performance Tips

  #34  
Jan 8th, 2008
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ Performance Tips

  #35  
Jan 8th, 2008
>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.
  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. }
I'm here to prove you wrong.
Reply With Quote  
Join Date: Feb 2008
Location: Singapore
Posts: 16
Reputation: sohguanh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
sohguanh sohguanh is offline Offline
Newbie Poster

Re: C++ Performance Tips

  #36  
Feb 28th, 2008
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ Performance Tips

  #37  
Feb 28th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Mar 2008
Location: India
Posts: 5
Reputation: patelmiteshb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
patelmiteshb's Avatar
patelmiteshb patelmiteshb is offline Offline
Newbie Poster

Re: C++ Performance Tips

  #38  
Apr 28th, 2008
Great Job Guys !! Thanks.
Reply With Quote  
Join Date: May 2008
Posts: 1
Reputation: AyaToujo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
AyaToujo AyaToujo is offline Offline
Newbie Poster

Re: C++ Performance Tips

  #39  
May 5th, 2008
Wow! I learned a lot of things from this thread! ... Thanks for the info everyone!!
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: Cait is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
Cait Cait is offline Offline
Newbie Poster

Re: C++ Performance Tips

  #40  
Jun 20th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:23 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC