Skip to content

Instantly share code, notes, and snippets.

@madisp
Created July 4, 2023 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madisp/31a298d191380d8a721c042d217fc36f to your computer and use it in GitHub Desktop.
Save madisp/31a298d191380d8a721c042d217fc36f to your computer and use it in GitHub Desktop.
Android CPU perf tests
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static java.math.BigInteger.ONE;
import org.junit.Test;
import java.math.BigInteger;
import java.util.stream.Stream;
public class CpuPerfTests {
public static final int NUM_COUNT = 200000;
public static final int CERTAINTY = 50;
@Test
public void computePrimes_singlethread() {
BigInteger sum = Stream.iterate(ONE, (x) -> x.add(ONE))
.filter(x -> x.isProbablePrime(CERTAINTY))
.limit(NUM_COUNT)
.reduce(BigInteger::add)
.get();
assertEquals(new BigInteger("264129169599"), sum);
}
@Test
public void computePrimes_multithread() {
BigInteger sum = Stream.iterate(ONE, (x) -> x.add(ONE))
.parallel()
.filter(x -> x.isProbablePrime(CERTAINTY))
.limit(NUM_COUNT)
.reduce(BigInteger::add)
.get();
assertEquals(new BigInteger("264129169599"), sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment