•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,097 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,319 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 39531 | Replies: 41
![]() |
•
•
Join Date: Apr 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
•
•
I don't think so. the result will be the fllowigs:
a=b;
b=0;
I am a beginnerplease 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!
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!
>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.
>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.
cplusplus Syntax (Toggle Plain Text)
#include <climits> #include <ctime> #include <iostream> void swap_temp ( int& a, int& b ) { int temp = a; a = b; b = temp; } void swap_xor ( int& a, int& b ) { a ^= b; b ^= a; a ^= b; } int main() { int a1 = 10, a2 = 20; int b1 = 10, b2 = 20; std::clock_t start; std::cout<< a1 <<"\t"<< a2 <<'\n'; std::cout<< b1 <<"\t"<< b2 <<'\n'; swap_temp ( a1, a2 ); swap_temp ( b1, b2 ); std::cout<< a1 <<"\t"<< a2 <<'\n'; std::cout<< b1 <<"\t"<< b2 <<'\n'; std::cout<<"swap_temp: "; start = std::clock(); for ( unsigned i = 0; i < UINT_MAX; i++ ) swap_temp ( a1, a2 ); std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n'; std::cout<<"swap_xor: "; start = std::clock(); for ( unsigned i = 0; i < UINT_MAX; i++ ) swap_temp ( b1, b2 ); std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n'; }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Feb 2008
Location: Singapore
Posts: 16
Reputation:
Rep Power: 1
Solved Threads: 2
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.
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.
>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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 1
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;
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 2:39 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- improve performance of the following io codes (C)
- Performance Improvements (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: computing determinant by upper triangular decomposition
- Next Thread: whats a cout?



please give me some advice
Linear Mode