This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class MyFinalClass { | |
private final Map<Long, Object> cache; | |
public MyClassFixed() { | |
cache = new HashMap<>(); | |
} | |
// final method | |
public Object get(long id) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClassOptimized { | |
private final Map<Long, Object> cache; | |
public MyClassFixed() { | |
cache = new HashMap<>(); | |
} | |
// non-virtual method | |
public final Object get(long id) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClassFixed { | |
private final Map<Long, Object> cache; | |
public MyClassFixed() { | |
// won't compile without this line | |
cache = new HashMap<>(); | |
} | |
public Object get(long id) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
private Map<Long, Object> cache; | |
public Object get(long id) { | |
return cache.get(id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
for d in ["configure", "Makefile", "lib.so"]: | |
os.system("touch " + d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LIB = my-lib | |
all: $(LIB)/lib.so | |
$(LIB)/lib.so: $(LIB)/Makefile | |
@cd $(LIB); make; | |
$(LIB)/Makefile: $(LIB)/configure | |
@cd $(LIB); ./configure | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double[] array = new double[LEN]; | |
for (int j = 0; j < array.length; j++) { | |
array[j] = Math.log(j); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void test() { | |
final int ROUNDS = 10; | |
long a1 = 0, a2 = 0, a3 = 0, t; | |
double[] array = new double[8388608]; | |
for (int i = 0; i < ROUNDS; i++) { | |
t = System.currentTimeMillis(); | |
attempt1(array); | |
a1 += System.currentTimeMillis() - t; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ForEach extends RecursiveAction { | |
private double[] array; | |
private int from; | |
private int to; | |
// you can fine-tune this, | |
// should be sth between 100 and 10000 | |
public final static int TASK_LEN = 5000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void attempt2(final double[] array) { | |
ExecutorService exec = Executors.newFixedThreadPool(NTHREADS - 1); | |
final int segmentLen = array.length / NTHREADS; | |
int offset = 0; | |
for (int i = 0; i < NTHREADS - 1; i++) { | |
final int from = offset; | |
final int to = offset + segmentLen; | |
exec.execute(new Runnable() { | |
@Override |
NewerOlder