File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33// tag::CACHE_EFFECT[]
44public class Caching {
5- private final int ARR_SIZE = 10 * 1024 * 1024 ;
5+ private final int ARR_SIZE = 2 * 1024 * 1024 ;
66 private final int [] testData = new int [ARR_SIZE ];
77
88 private void run () {
Original file line number Diff line number Diff line change @@ -8,21 +8,22 @@ public static void main(String[] args) {
88 }
99
1010 void run () {
11- for (int i = 1 ; i < 2_000 ; i ++) {
11+ for (int i = 1 ; i < 2_000 ; i ++) {
1212 timeHashing (i , 'x' );
1313 }
1414 }
1515
1616
1717 void timeHashing (int length , char c ) {
1818 final StringBuilder sb = new StringBuilder ();
19- for (int j = 0 ; j < length * 1_000_000 ; j ++) {
19+ for (int j = 0 ; j < length * 1_000_000 ; j ++) {
2020 sb .append (c );
2121 }
2222 final String s = sb .toString ();
2323 final long now = System .nanoTime ();
2424 final int hash = s .hashCode ();
2525 final long duration = System .nanoTime () - now ;
26- System .out .println ("Length: " + length +" took: " + duration +" ns" );
26+ // System.out.println("Length: " + length + " took: " + duration + " ns");
27+ System .out .println (length + "," + duration );
2728 }
2829}
Original file line number Diff line number Diff line change 33import sun .misc .Unsafe ;
44import java .lang .reflect .Field ;
55
6- public final class AtomicCounter {
6+ public final class AtomicCounter implements Counter {
77
88 private static final Unsafe unsafe ; // = Unsafe.getUnsafe();
99 private static final long valueOffset ;
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ public class CounterMain {
88 public static final int REPS = 10_000_000 ;
99
1010 public static void main (String [] args ) throws InterruptedException {
11- final Counter c = new UnprotectedCounter (); // init to 0
11+ final Counter c = new AtomicCounter (); // init to 0
1212
1313 Runnable r = () -> {
1414 for (int i = 0 ; i < REPS ; i ++) {
You can’t perform that action at this time.
0 commit comments