@RyanMarcus/

ArrayList vs Array

Java

No description

fork
loading
Files
  • Main.java
  • Main.class
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;

class Main {
  public static void main(String[] args) {
    List<Integer> l = new ArrayList<Integer>(50000);

    long start = System.currentTimeMillis();
    for (int i = 0; i < 50000; i++) {
      l.add(i*10);
    }

    long accum = 0;
    for (int i = 0; i < 50000; i++) {
      accum += l.get(i) / 2;
    }
    long stop = System.currentTimeMillis();

    System.out.println(stop - start);

    int[] a = new int[50000];
    start = System.currentTimeMillis();
    for (int i = 0; i < 50000; i++) {
      a[i] = i*10;
    }

    accum = 0;
    for (int i = 0; i < 50000; i++) {
      accum += a[i] / 2;
    }
    stop = System.currentTimeMillis();

    System.out.println(stop - start);
  }
}