Created
August 16, 2016 08:18
-
-
Save c0rp-aubakirov/d07a75e90e838621d4d94977037e582e to your computer and use it in GitHub Desktop.
This file contains 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 kz.kaznu.counter; | |
import com.google.common.base.Stopwatch; | |
import kz.kaznu.counter.commons.utils.LoggerEnabler; | |
import kz.kaznu.counter.commons.utils.Timer; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Random; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* User: Sanzhar Aubakirov | |
* Date: 8/16/16 | |
*/ | |
public class Test { | |
public static final int NUMBER_OF_TIMES_TO_TEST = 1000; | |
public static long timeDirectDelete = 0; | |
public static long timeCreateNewArray = 0; | |
private final Random random = new Random(); | |
@org.junit.Test | |
public void test() throws Exception { | |
for (int i = 0; i < NUMBER_OF_TIMES_TO_TEST; i++) { | |
testDeleteOneElement(); | |
} | |
System.out.println(timeDirectDelete/NUMBER_OF_TIMES_TO_TEST+ "\t" + timeCreateNewArray/NUMBER_OF_TIMES_TO_TEST); | |
} | |
public void testDeleteOneElement() throws Exception { | |
final ArrayList<Double> array1 = new ArrayList<>(); | |
for (int i = 0; i < 100000; i++) { | |
array1.add(random.nextDouble() * 100000); | |
} | |
timeDirectDelete += executeVoidFunctionWithTimer(() -> deleteFromArray(array1)); | |
final ArrayList<Double> array2 = new ArrayList<>(); | |
for (int i = 0; i < 100000; i++) { | |
array2.add(random.nextDouble() * 100000); | |
} | |
timeCreateNewArray += executeVoidFunctionWithTimer(() -> createNewArrayWithoutElement(array2)); | |
} | |
private Void deleteFromArray(ArrayList<Double> arrayList) { | |
for (Iterator<Double> iterator = arrayList.iterator(); iterator.hasNext();) { | |
final Double aDouble = iterator.next(); | |
if (aDouble >= 99900) { | |
// Remove the current element from the iterator and the list. | |
iterator.remove(); | |
} | |
} | |
// System.out.println("DirectDeleteFromArray\t Now size of array is\t" + arrayList.size()); | |
return null; | |
} | |
public Void createNewArrayWithoutElement(ArrayList<Double> arrayList) { | |
Object[] objects = arrayList.stream().filter(aDouble -> aDouble < 99900).toArray(); | |
// System.out.println("CreateNewArray\tNow size of array is\t" + objects.length); | |
return null; | |
} | |
public static <R> long executeVoidFunctionWithTimer(Callable<R> f) throws Exception { | |
final Stopwatch timer = Stopwatch.createUnstarted(); | |
timer.start(); | |
R r = f.call(); | |
timer.stop(); | |
return timer.elapsed(TimeUnit.MICROSECONDS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment