Important alert: (current site time 7/15/2013 3:35:38 PM EDT)
 

VB icon

[^ Test which swapping method is the best ^]

Email
Submitted on: 8/16/2003 4:23:20 PM
By: Donny Nadolny 
Level: Beginner
User Rating: By 3 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 11097
(About the author)
 
     When you run this program it will show you which method of swapping two variables is the best (using XOR, a third temporary variable, or the Addition/Subtraction method) so your programs can use the most optimized technique.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: [^ Test which swapping method is the best ^]
// Description:When you run this program it will show you which method of swapping two variables is the best (using XOR, a third temporary variable, or the Addition/Subtraction method) so your programs can use the most optimized technique.
// By: Donny Nadolny
//
// Returns:Shows the time (in millaseconds) to swap using 4 different techniques
//
// Assumes:All you need to do is run the program and read the output.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3824&lngWId=2//for details.//**************************************

/*
 * This class will test how fast the different ways of swapping variables are.
 * You can then choose the best way of swapping to get the best speed.
 * Made by Donny Nadolny (just the code to test these different ways of swapping,
 * the swapping methods themselves weren't made by me)
 *
 * On my computer (AMD Athlon 1.6GHz), the times are as follows:
 * 1. 390 ms - Swapping using a temp variable where it's declared once
 * 2. 421 ms - Swapping using a temp variable where it's declared each swap
 * 3. 581 ms - Swapping using the XOR way
 * 4. 691 ms - Swapping using the adition/subtraction way
 *
 * Note that these may vary depending on your computer speed and what programs are open.
 */
public class TestSwap
{
public static void main (String [] args)
{
//Number of times to swap
int times = 5000000;
//First int to swap
int variable1 = 5;
//Second int to swap
int variable2 = 6;
//The time (in millaseconds) before the variables were swapped
long before;
//The time (in millaseconds) after the variables were swapped
long after;
before = System.currentTimeMillis ();
//Swap them many times using the XOR way
for (int i = 0 ; i < times ; i++)
{
variable1 ^= variable2;
variable2 ^= variable1;
variable1 ^= variable2;
}
after = System.currentTimeMillis ();
//Print out how long it took
System.out.println ("It takes " + (after - before) + " milaseconds to swap variables using the XOR method.");
before = System.currentTimeMillis ();
//Swap many times using a temporary variable where it's declared each swap
for (int i = 0 ; i < times ; i++)
{
int tempVariable;
tempVariable = variable1;
variable1 = variable2;
variable2 = tempVariable;
}
after = System.currentTimeMillis ();
//Print out how long it took
System.out.println ("It takes " + (after - before) + " milaseconds to swap variables using a temporary variable where the variable is declared each time it is used to swap.");
before = System.currentTimeMillis ();
//Create a temporary variable once
int tempVariable;
//Swap using the temporary variable where it's declared only once
for (int i = 0 ; i < times ; i++)
{
tempVariable = variable1;
variable1 = variable2;
variable2 = tempVariable;
}
after = System.currentTimeMillis ();
//Print out how long it took
System.out.println ("It takes " + (after - before) + " milaseconds to swap variables using a temporary variable where the variable is declared once and used to swap many times.");
before = System.currentTimeMillis ();
//Swap using the addition/subtraction way many times
for (int i = 0 ; i < times ; i++)
{
variable1 += variable2;
variable2 = variable1 - variable2;
variable1 -= variable2;
}
after = System.currentTimeMillis ();
//Print out how long it took
System.out.println ("It takes " + (after - before) + " milaseconds to swap variables using the adition/subtraction method.");
}
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/22/2003 6:38:12 PMYehia Muhsen

I tried that code and it showed results similar to yours. In general, using the addition and subtraction is the slowest method, and using the XOR method is about 1.5 times faster, whereas using the temporary variable is about twice times faster. That was amazing results. At the begining I thought that using new memory location will make the code slower, but it turned out the opposite... Nice finding and nice code .
(If this comment was disrespectful, please report it.)

 
4/1/2008 11:44:23 AMDouglas G. Allen

my computer (AMD Athlon 1.8GHz, 256MB),run SwapTest.java in NetBeans and results varied too much to tell.

Perhaps threading these, one at a time, may tell us more.
Thanks!
(If this comment was disrespectful, please report it.)

 
1/9/2009 4:31:18 AMcode proj

code planet
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.