After reading the article Mechanism and characteristics of Collection implementation class often used in Java, I used arrayList to implement the mechanism I am trying to create. I was wondering which of the linkedList is better, so I will write the result of checking the calculation time.
What I tried to make was a "route array".
After securing the array, search for the elements in the path and extract the smallest value from the range that meets the conditions (for example, the range that cannot be determined unless the entire array is determined, such as the 10 numbers in the middle of the array). I am aiming for it.
Based on the above, consider the class to use.
-Check the speed of element search of linkedList after creating an array.
-If the calculation speed is slow, compare it with the array addition speed of arraylist.
-If the calculation speed can be tolerated, implement it using the linked list as it is.
Therefore, the following code is used for comparison using the for statement and the extended for statement.
test.java
import java.util.LinkedList;
import java.util.Iterator;
public class test {
static final int LIMIT = 100_000;
static LinkedList<Integer> array = new LinkedList<Integer>();
static long sum = 0;
static {
for (int i = 0; i < LIMIT; i++) {
array.addLast(i);
}
}
static void addArrayNumbersWithFor() {
sum = 0L;
for (int i = 0; i < LIMIT; i++) {
sum += array.get(i);
}
}
static void addArrayNumbersWithAppliedFor() {
sum = 0L;
for (int value : array) {
sum += value;
}
}
static double measureCalculationTimeWithFor() {
//Get start time in milliseconds
long ts1 = System.currentTimeMillis();
//Processing execution
addArrayNumbersWithFor();
//Get end time in milliseconds
long te1 = System.currentTimeMillis();
//Processing time milliseconds
long tmsec1 = te1 - ts1;
//Processing time seconds
double tsec1 = (double) tmsec1 / 1000.0;
System.out.println(sum);
return tsec1;
}
static double measureCalculationTimeWithAppliedFor() {
//Get start time in milliseconds
long ts1 = System.currentTimeMillis();
//Processing execution
addArrayNumbersWithAppliedFor();
//Get end time in milliseconds
long te1 = System.currentTimeMillis();
//Processing time milliseconds
long tmsec1 = te1 - ts1;
//Processing time seconds
double tsec1 = (double) tmsec1 / 1000.0;
System.out.println(sum);
return tsec1;
}
public static void main(String[] args) {
System.out.println(test.measureCalculationTimeWithAppliedFor());
System.out.println(test.measureCalculationTimeWithFor());
}
}
From this result, when a simple for statement is used, the elements are always acquired in order from the first element in the loop, whereas in the case of the extended for statement, the information of the next element is obtained from the previous element. You can see that it gets and gets the next element.
If you want to add elements to an array one after another and you don't know how many to add, you'll want to use linkedlist. The bottleneck is the slow speed of random access, but it can be seen that there is no slow speed as the elements are acquired in order from the beginning by using the extended for statement. I haven't compared it with an ordinary array, but it's based on the premise that the collection type is used.
Recommended Posts