Created
February 10, 2016 21:34
-
-
Save arterzatij/725178e0cdf3f19f8d19 to your computer and use it in GitHub Desktop.
vector vs arraylist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package test; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
/** | |
* @author earteaga | |
* | |
*/ | |
@RunWith(Parameterized.class) | |
public class ArrayListTest { | |
@Parameters | |
public static Collection<Object[]> data() { | |
return Arrays.asList( | |
new Object[][] { | |
{ 10, 10 }, | |
{ 10, 100 }, | |
{ 10, 1000 }, | |
{ 100, 10 }, | |
{ 100, 100 }, | |
{ 100, 1000 }, | |
{ 1000, 10 }, | |
{ 1000, 100 }, | |
{ 1000, 1000 }, | |
{ 10000, 10 }, | |
{ 10000, 100 }, | |
{ 10000, 1000 }, | |
{ 100000, 10 }, | |
{ 100000, 100 }, | |
{ 100000, 1000 }, | |
{ 1000000, 10 }, | |
{ 1000000, 100 }, | |
{ 1000000, 1000 } | |
}); | |
} | |
@Test | |
public void test() { | |
long t = 0; | |
for(int j = 0; j < testCicles; j++) { | |
t0 = System.currentTimeMillis(); | |
arrayList = new ArrayList<Integer>(dataQTY); | |
for(int i = 0; i < dataQTY; i++) { | |
arrayList.add(i); | |
} | |
t = System.currentTimeMillis() - t0; | |
tt += t; | |
//System.out.printf("%d elementos en ArrayList tarda %d%n", dataQTY, t); | |
} | |
} | |
/** | |
* @throws java.lang.Exception | |
*/ | |
@After | |
public void tearDown() throws Exception { | |
arrayList.clear(); | |
arrayList = null; | |
System.out.printf("%d ciclos de insercion de %d elementos en ArrayList tarda %d%n", testCicles, dataQTY, tt); | |
} | |
/** | |
* @param dataQTY | |
* @param testCicles | |
* @param incRate | |
*/ | |
public ArrayListTest(int dataQTY, int testCicles) { | |
super(); | |
this.dataQTY = dataQTY; | |
this.testCicles = testCicles; | |
} | |
private int dataQTY; | |
private int testCicles; | |
private long t0; | |
private long tt = 0; | |
private ArrayList<Integer> arrayList; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package test; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Vector; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
/** | |
* @author earteaga | |
* | |
*/ | |
@RunWith(Parameterized.class) | |
public class VectorTest { | |
@Parameters | |
public static Collection<Object[]> data() { | |
return Arrays.asList( | |
new Object[][] { | |
{ 10, 10 }, | |
{ 10, 100 }, | |
{ 10, 1000 }, | |
{ 100, 10 }, | |
{ 100, 100 }, | |
{ 100, 1000 }, | |
{ 1000, 10 }, | |
{ 1000, 100 }, | |
{ 1000, 1000 }, | |
{ 10000, 10 }, | |
{ 10000, 100 }, | |
{ 10000, 1000 }, | |
{ 100000, 10 }, | |
{ 100000, 100 }, | |
{ 100000, 1000 }, | |
{ 1000000, 10 }, | |
{ 1000000, 100 }, | |
{ 1000000, 1000 } | |
}); | |
} | |
@Test | |
public void test() { | |
long t = 0; | |
for(int j = 0; j < testCicles; j++) { | |
t0 = System.currentTimeMillis(); | |
vector = new Vector<Integer>(dataQTY, (int) (dataQTY * INC_RATE)); | |
for(int i = 0; i < dataQTY; i++) { | |
vector.add(i); | |
} | |
vector.clear(); | |
t = System.currentTimeMillis() - t0; | |
tt += t; | |
//System.out.printf("%d elementos en ArrayList tarda %d%n", dataQTY, t); | |
} | |
} | |
/** | |
* @throws java.lang.Exception | |
*/ | |
@After | |
public void tearDown() throws Exception { | |
vector = null; | |
System.out | |
.printf( | |
"%d ciclos de insercion de %d elementos en Vector tarda %d, con %d porciento de incremento en la capacidad incial\n", | |
testCicles, dataQTY, tt, (int) (INC_RATE * 100)); | |
} | |
/** | |
* @param dataQTY | |
* @param testCicles | |
* @param incRate | |
*/ | |
public VectorTest(int dataQTY, int testCicles) { | |
super(); | |
this.dataQTY = dataQTY; | |
this.testCicles = testCicles; | |
} | |
private int dataQTY; | |
private int testCicles; | |
private long t0; | |
private long tt = 0; | |
private Vector<Integer> vector; | |
private static final double INC_RATE = 1.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment