Skip to content

Instantly share code, notes, and snippets.

@hristo-vrigazov
Last active December 30, 2020 10:45
Show Gist options
  • Save hristo-vrigazov/d51d496cb25bd7d0a80062f46a7e4df4 to your computer and use it in GitHub Desktop.
Save hristo-vrigazov/d51d496cb25bd7d0a80062f46a7e4df4 to your computer and use it in GitHub Desktop.
Example compare Java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
List<Long> times = new ArrayList<>();
int numExperiments = 20;
long[] totals = new long[numExperiments];
Random random = new Random();
for (int exp = 0; exp < numExperiments; exp++) {
long start = System.currentTimeMillis();
int[] arr = new int[100000000];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(1000);
}
long generationTook = System.currentTimeMillis() - start;
System.out.println("Generation took " + (generationTook / 1000.));
start = System.currentTimeMillis();
long total = 0;
for (int i = 0; i < arr.length; i++) {
if ((arr[i] > 0) && (arr[i] < 100) || (arr[i] == 170)) {
total += arr[i];
}
}
totals[exp] = total;
long took = System.currentTimeMillis() - start;
System.out.println("Processing array took " + (took / 1000.));
times.add(took);
}
System.out.println(times.stream().mapToLong(i -> i).average().orElse(0) / 1000.);
}
}
@hristo-vrigazov
Copy link
Author

Generation took 1.154
Processing array took 1.287
Generation took 0.946
Processing array took 1.079
Generation took 0.93
Processing array took 1.073
Generation took 0.984
Processing array took 1.13
Generation took 0.983
Processing array took 1.128
Generation took 0.979
Processing array took 1.125
Generation took 0.969
Processing array took 1.108
Generation took 0.941
Processing array took 1.077
Generation took 0.961
Processing array took 1.108
Generation took 0.953
Processing array took 1.097
Generation took 0.938
Processing array took 1.084
Generation took 0.959
Processing array took 1.104
Generation took 0.976
Processing array took 1.117
Generation took 0.948
Processing array took 1.091
Generation took 0.99
Processing array took 1.123
Generation took 0.946
Processing array took 1.081
Generation took 0.932
Processing array took 1.074
Generation took 0.971
Processing array took 1.107
Generation took 0.936
Processing array took 1.07
Generation took 0.958
Processing array took 1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment