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