LinkedList info

master
Ivan Zahariev 2016-09-22 22:15:51 +03:00
parent bdcef85363
commit 5851ea6456
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,30 @@
--- ../primes.java 2016-09-22 22:10:58.787516331 +0300
+++ primes.java 2016-09-22 22:11:14.919516331 +0300
@@ -2,15 +2,15 @@
import java.lang.Math;
class PrimeNumbersGenerator {
- ArrayList<Integer> get_primes7(int n) {
- ArrayList<Integer> res = new ArrayList<Integer>();
+ List<Integer> get_primes7(int n) {
+ List<Integer> res = new LinkedList<Integer>();
if (n < 2) return res;
if (n == 2) {
res.add(2);
return res;
}
- ArrayList<Integer> s = new ArrayList<Integer>();
+ List<Integer> s = new ArrayList<Integer>();
for (int i = 3; i < n + 1; i += 2) {
s.add(i);
}
@@ -46,7 +46,7 @@
long startTime = System.currentTimeMillis();
long periodTime = Long.parseLong(System.getenv("RUN_TIME"), 10) * 1000;
- ArrayList<Integer> res;
+ List<Integer> res;
while ((System.currentTimeMillis() - startTime) < periodTime) {
res = (new PrimeNumbersGenerator()).get_primes7(10000000);

View File

@ -4,5 +4,6 @@ Java can be (a lot) faster than what we see it with its default interpeter setti
* Initial heap size -- setting this larger than the default value will allow the ArrayList to grow more efficiently by pre-allocating a bigger memory block from the system. The "java" command-line argument is "-Xms500m".
* Heap and GC tuning -- we can control the ratio between the old and young generations. The "java" command-line argument is "-XX:NewRatio=1".
* Skip the unneeded boxing/unboxing -- this requires a more native Java implementation. See the source code of "[primes-alt.java](../primes-alt.java)".
* Use LinkedList -- it has O(1) complexity for adding elements. Benchmark tests however don't show significant improvement under Java 8, and under Java 7 we see a slow down of 59%.
These tweaks are not included in the Java benchmark test, because I wanted to test the default languages setup. I don't aim to fine-tune any of the implementations by optimizing them for the current task/algorithm. This is a generic test, not an attempt to complete the current task in the fastest possible way for each programming language.