mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-25 22:38:04 +02:00
misc
This commit is contained in:
parent
1cc20da665
commit
b18561fc54
19 changed files with 197 additions and 0 deletions
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,78 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class MultipleCorePrimeTest {
|
||||
public static void count(int target, int threads)
|
||||
throws InterruptedException, TimeoutException {
|
||||
ExecutorService executor =
|
||||
Executors.newFixedThreadPool(threads);
|
||||
List<FutureTask<Integer>> taskList =
|
||||
new ArrayList<FutureTask<Integer>>();
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
for (int i = 1; i <= threads; ++i) {
|
||||
int ilen = target / threads;
|
||||
|
||||
/* Test following intervall for primes */
|
||||
final int start = (i - 1) * ilen;
|
||||
final int end = (i != threads)
|
||||
? i * ilen - 1
|
||||
: target;
|
||||
FutureTask<Integer> task =
|
||||
new FutureTask<Integer>(
|
||||
new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() {
|
||||
int count = 0;
|
||||
for (int i = start; i <= end;
|
||||
++i) {
|
||||
if (SingleCorePrimeTest.
|
||||
isPrime(i))
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
});
|
||||
taskList.add(task);
|
||||
executor.submit(task);
|
||||
}
|
||||
|
||||
executor.shutdown();
|
||||
if (!executor.awaitTermination(10,
|
||||
TimeUnit.MINUTES)) {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
final long endTime = System.currentTimeMillis();
|
||||
int count = 0;
|
||||
for (int i = 0; i < taskList.size(); ++i) {
|
||||
try {
|
||||
count += taskList.get(i).get();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(threads + " thread: "
|
||||
+ (endTime - startTime) + " ms");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int target = 100_000_000;
|
||||
try {
|
||||
count(target, 1);
|
||||
count(target, 2);
|
||||
count(target, 4);
|
||||
count(target, 8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
import java.util.Arrays;
|
||||
|
||||
public class SieveOfErasthostenes {
|
||||
|
||||
public static boolean[] sieveIt(int n) {
|
||||
boolean[] sieve = new boolean[n+1];
|
||||
Arrays.fill(sieve, true);
|
||||
sieve[0] = false;
|
||||
sieve[1] = false;
|
||||
for (int i=2; i <= Math.sqrt(n); i++) {
|
||||
if (sieve[i]) {
|
||||
for (int c = i*i; c <= n; c += i) {
|
||||
sieve[c] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sieve;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final int n = 100_000_000;
|
||||
final long startTime = System.currentTimeMillis();
|
||||
sieveIt(n);
|
||||
final long endTime = System.currentTimeMillis();
|
||||
System.out.println(endTime-startTime);
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
/* @author Axel Busch */
|
||||
public class SingleCorePrimeTest {
|
||||
|
||||
public static boolean isPrime(int n) {
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 2; i <= Math.sqrt(n); ++i) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 10_000_000;
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 2; i <= target; ++i) {
|
||||
isPrime(i);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println(end-start);
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,35 @@
|
|||
import scala.actors.Futures._
|
||||
/** @author Axel Busch */
|
||||
object EratosthenesFutures {
|
||||
def parallel(end : Int, threads: Int) : Array[Boolean] = {
|
||||
val sieve = Array.fill[Boolean](end+1)(true)
|
||||
for (i <- 2 to scala.math.sqrt(end).toInt) {
|
||||
if (sieve(i)) {
|
||||
val nextComposite = i*i
|
||||
val compositesPerThread = (end-nextComposite+1)/(threads*i)
|
||||
val tasks = for (t <- 0 until threads) yield future {
|
||||
val begin = nextComposite + t * i *
|
||||
compositesPerThread
|
||||
val finish = if (t+1 == threads) end
|
||||
else nextComposite + (t+1) * i *
|
||||
compositesPerThread
|
||||
assert (begin % i == 0)
|
||||
for (composite <- begin to finish by i) {
|
||||
sieve(composite) = false
|
||||
}
|
||||
}
|
||||
awaitAll(20000L, tasks: _*);
|
||||
}
|
||||
}
|
||||
sieve
|
||||
}
|
||||
def main(args: Array[String]) = {
|
||||
val end = 100000000
|
||||
for (threads <- List(1,2,4,8)) {
|
||||
val startTime = System.currentTimeMillis();
|
||||
parallel(end,threads);
|
||||
val endTime = System.currentTimeMillis();
|
||||
println(threads + " thread: " + (endTime - startTime) + " ms");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue