Giter VIP home page Giter VIP logo

hsdis's People

Contributors

bitcharmer avatar

hsdis's Issues

Fast

public class Main {

public static final long TOKENS = 10_000_000_000L;
public static final int ITERATIONS = 150_000;
public static final int POST_BACKOFF_ITERS = 20;
public static final int RESULT_BUFFER_SIZE = 128;
public static final int RESULT_BUFFER_MASK = RESULT_BUFFER_SIZE - 1;
private static long[] results = new long[RESULT_BUFFER_SIZE];

public static void main(String[] args) {
    System.loadLibrary("native");

    long now = measure();
    printResults();

    System.err.println("counter: " + now);
}

private static void printResults() {
    int offset = ITERATIONS + POST_BACKOFF_ITERS - RESULT_BUFFER_SIZE;
    for (int i = 0; i < RESULT_BUFFER_SIZE; i++)
        System.err.printf("Iteration %d: %d ns\n", i + 1 + offset, results[(offset + i) & RESULT_BUFFER_MASK]);
}

private static long measure() {
    Native.setAffinity(7);

    long now = 0;
    for (int i = 0; i < ITERATIONS + POST_BACKOFF_ITERS; i++) {
        backOff(i);
        now = timeInvocation(i);
    }

    return now;
}

private static void backOff(int i) {
    final long backOffMask = -((ITERATIONS - i) >>> 63);
    Blackhole.consumeCPU(1L + (TOKENS & backOffMask));
}

private static long timeInvocation(int i) {
    long now;
    final long start = System.nanoTime();
    now = System.nanoTime();
    final long end = System.nanoTime();
    storeResult(i, end - start);
    return now;
}

private static void storeResult(int i, long l) {
    final int idx = i & RESULT_BUFFER_MASK;
    results[idx] = l;
    System.err.printf("iteration %d: %d\n", i, l);
}

}

Sample

public class Main {

public static final long TOKENS = 1_000_000_000L;
public static final int ITERATIONS = 150_000;
public static final int POST_BACKOFF_ITERS = 30;
public static final int RESULT_BUFFER_SIZE = 128;
public static final int RESULT_BUFFER_MASK = RESULT_BUFFER_SIZE - 1;
private static long[] results = new long[RESULT_BUFFER_SIZE];

public static void main(String[] args) {
    final long l = 0xAAAAAAAAL;
    System.out.println("" + Long.toBinaryString(l));
    System.exit(1);
    System.loadLibrary("native");

    long now = measure();

// printResults();

    System.err.println("counter: " + now);
}

private static void printResults() {
    int offset = ITERATIONS + POST_BACKOFF_ITERS - RESULT_BUFFER_SIZE;
    for (int i = 0; i < RESULT_BUFFER_SIZE; i++)
        System.err.printf("Iteration %d: %d ns\n", i + 1 + offset, results[(offset + i) & RESULT_BUFFER_MASK]);
}

private static long measure() {
    Native.setAffinity(7);
    long now = 0;
    for (int i = 0; i < ITERATIONS + POST_BACKOFF_ITERS; i++) {
        backOff(i);
        now = timeInvocation(i);
    }
    return now;
}

private static void backOff(int i) {
    final long backOffMask = -((ITERATIONS - i) >>> 63);
    Blackhole.consumeCPU(1L + (TOKENS & backOffMask));
}

private static long timeInvocation(int i) {
    long now;
    final long start = System.nanoTime();

// Blackhole.consumeCPU(500);
now = System.nanoTime();
final long end = System.nanoTime();
storeResult(i, end - start);
return now;
}

private static void storeResult(int i, long l) {

// final int idx = i & RESULT_BUFFER_MASK;
// results[idx] = l;
System.err.printf("iteration %d: %d\n", i, l);
if (l < 0) {
System.err.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
System.exit(1);
}
}

private static void storeResult2(int i, long start, long end) {
    final long l = end - start;

// final int idx = i & RESULT_BUFFER_MASK;
// results[idx] = l;
System.err.printf("iteration %d: %d\n", i, l);
if (l < 0) {
System.err.printf("start:\t%d\n", start);
System.err.printf("end\t%d\n", end);
System.exit(1);
}
}

}

Slow

import org.openjdk.jmh.infra.Blackhole;

public class NanoTimeTest {

private final long preIters;
private final long tokens;
private long iteration;

public NanoTimeTest(long preIters, long tokens) {
    this.preIters = preIters;
    this.tokens = tokens;
}

public long performWork() {
    return System.nanoTime();
}

public void beforeIteration() {
    final long backOffMask = -((preIters - iteration++) >>> 63);
    Blackhole.consumeCPU(1L + (tokens & backOffMask));
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.