Skip to content

Commit

Permalink
Perf of IntList verified, keep.
Browse files Browse the repository at this point in the history
  • Loading branch information
glorieux-f committed Aug 18, 2024
1 parent a5d8507 commit c1d4b0c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/github/oeuvres/alix/util/IntList.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import com.github.oeuvres.alix.maths.Calcul;

/**
* A mutable list of ints.
* A mutable list of ints. Tested as 2x faster than ArrayList<Integer> for insertion,
* 10x for mutable operations like {@link #inc(int)} {@link #add(int, int)}. Lighter in memory,
* Integer is 20 bytes, int is 4.
*/
public class IntList
{
Expand Down
61 changes: 60 additions & 1 deletion src/test/java/com/github/oeuvres/alix/util/IntListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.junit.Test;

public class IntListTest {

@Test
static Random RANDOM = new Random();
static int SIZE = 1000;
static final int MAXFORM = 25000;
static final int LOOPS = 10;
static final int OPS = 1000;


public void uniq()
{
int[] uniq;
Expand All @@ -34,4 +43,54 @@ public void uniq()
assertArrayEquals(""+Arrays.toString(uniq) + "≠" + Arrays.toString(exp6), exp6, uniq);
}

static public void ints()
{
System.out.println("IntList");
IntList ints = new IntList();
for (int loop = 0; loop < LOOPS; loop++) {
ints.clear();
long start = System.nanoTime();
for (int pos = 0; pos < SIZE; pos++) {
ints.push(RANDOM.nextInt(MAXFORM));
}
double laps = ((double)(System.nanoTime() - start) / 1000000);
System.out.print(" loading=" + laps + " ms. ");
for (int op = 0; op < OPS; op++) {
final int pos = RANDOM.nextInt(SIZE);
ints.inc(pos);
}
laps = ((double)(System.nanoTime() - start) / 1000000);
System.out.println(" inc=" + laps + " ms. ");
}
System.out.println();
}

static public void arrayList()
{
System.out.println("ArrayList");
List<Integer> ints = new ArrayList<>();
for (int loop = 0; loop < LOOPS; loop++) {
ints.clear();
long start = System.nanoTime();
for (int pos = 0; pos < SIZE; pos++) {
ints.add(RANDOM.nextInt(MAXFORM));
}
double laps = ((double)(System.nanoTime() - start) / 1000000);
System.out.print(" loading=" + laps + " ms. ");
for (int op = 0; op < OPS; op++) {
final int pos = RANDOM.nextInt(SIZE);
ints.add(pos, ints.get(pos) + 1);
}
laps = ((double)(System.nanoTime() - start) / 1000000);
System.out.println(" inc=" + laps + " ms. ");
}
System.out.println();
}

public static void main(String[] args)
{
ints();
arrayList();
}
}

0 comments on commit c1d4b0c

Please sign in to comment.