Example usage for java.lang System nanoTime

List of usage examples for java.lang System nanoTime

Introduction

In this page you can find the example usage for java.lang System nanoTime.

Prototype

@HotSpotIntrinsicCandidate
public static native long nanoTime();

Source Link

Document

Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

Usage

From source file:Main.java

public static void main(String[] args) {
    long start = System.nanoTime();
    // block of code to time
    long end = System.nanoTime();
    System.out.println("It took " + (end - start) + " nanoseconds");

}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    long start = System.nanoTime();
    System.out.println("Start: " + start);

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            System.out.println("asdf");
        }//from   w w  w  . j  a v  a  2 s.c  om
    }

    long end = System.nanoTime();
    System.out.println("End  : " + end);

    long elapsedTime = end - start;

    System.out.println(elapsedTime + " nano seconds");
}

From source file:Main.java

public static void main(String[] argv) {
    System.out.println("Benchmarking...");
    long t0 = System.nanoTime();

    int a[] = IntStream.range(0, 1_000_000).filter(p -> p % 2 == 0).toArray();
    long t1 = System.nanoTime();

    int b[] = IntStream.range(0, 1_000_000).parallel().filter(p -> p % 2 == 0).toArray();
    long t2 = System.nanoTime();

    System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9);
}

From source file:Main.java

public static void main(String[] args) {

    // returns the current value of the system timer, in nanoseconds
    System.out.print("time in nanoseconds = ");
    System.out.println(System.nanoTime());

}

From source file:Main.java

public static void main(String... args) {
    final int MAX_VAL = 99999;
    List<Integer> linkedList = new LinkedList<Integer>();
    List<Integer> arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);//w w  w. java  2 s . co m
        arrayList.add(i);
    }
    long time = System.nanoTime();

    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(MAX_VAL / 2, i);
    }
    System.out.println("LinkedList add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    for (int i = 0; i < MAX_VAL; i++) {
        arrayList.add(MAX_VAL / 2, i);
    }
    System.out.println("ArrayList add:" + (System.nanoTime() - time));

    // Reset the lists
    linkedList = new LinkedList<Integer>();
    arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);
        arrayList.add(i);
    }

    time = System.nanoTime();
    ListIterator<Integer> li = linkedList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        li.add(i);
    }
    System.out.println("LinkedList iterator add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    ListIterator<Integer> ali = arrayList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        ali.add(i);
    }
    System.out.println("ArrayList iterator add:" + (System.nanoTime() - time));
}

From source file:Main.java

public static void main(String[] args) {
    ForkJoinPool fjPool = new ForkJoinPool();
    int[] a = new int[3333344];
    for (int i = 0; i < a.length; i++) {
        int k = (int) (Math.random() * 22222);
        a[i] = k;//from w  ww. ja  v  a  2 s.c  om
    }
    ForkJoinQuicksortTask forkJoinQuicksortTask = new ForkJoinQuicksortTask(a, 0, a.length - 1);
    long start = System.nanoTime();
    fjPool.invoke(forkJoinQuicksortTask);
    System.out.println("Time: " + (System.nanoTime() - start));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int size = 100000;
    int[] v1 = new int[size];
    for (int i = 0; i < size; i++) {
        v1[i] = i;//from  w w w .ja  v a2s . c o  m
    }
    for (SEQ_THRESHOLD = 10; SEQ_THRESHOLD < size; SEQ_THRESHOLD += 50) {
        double avgTime = 0.0;
        int samples = 5;
        for (int i = 0; i < samples; i++) {
            long startTime = System.nanoTime();
            ForkJoinPool fjp = new ForkJoinPool();
            fjp.invoke(new MyAction(0, size, v1));
            long endTime = System.nanoTime();
            double secsTaken = (endTime - startTime) / 1.0e9;
            avgTime += secsTaken;
        }
        System.out.println(SEQ_THRESHOLD + " " + (avgTime / samples));
    }
}

From source file:mase.deprecated.FastMathTest.java

public static void main(String[] args) {
    double MIN = -10;
    double MAX = 10;
    int N = 10000;
    DescriptiveStatistics diff = new DescriptiveStatistics();
    DescriptiveStatistics diffJava = new DescriptiveStatistics();
    long tFast = 0, tNormal = 0, tBounded = 0, tJava = 0;
    for (int i = 0; i < N; i++) {
        double x = Math.random() * (MAX - MIN) + MIN;
        long t = System.nanoTime();
        double v1 = (1.0 / (1.0 + FastMath.expQuick(-1 * x)));
        tFast += System.nanoTime() - t;
        t = System.nanoTime();//  w w  w .  ja  v  a2  s .co  m
        double v2 = (1.0 / (1.0 + FastMath.exp(-1 * x)));
        tNormal += System.nanoTime() - t;
        t = System.nanoTime();
        double v3 = (1.0 / (1.0 + BoundMath.exp(-1 * x)));
        tBounded += System.nanoTime() - t;
        t = System.nanoTime();
        double v4 = (1.0 / (1.0 + Math.exp(-1 * x)));
        tJava += System.nanoTime() - t;
        diff.addValue(Math.abs(v1 - v2));
        diffJava.addValue(Math.abs(v3 - v1));
    }

    System.out.println("MAX: " + diff.getMax());
    System.out.println("MEAN: " + diff.getMean());
    System.out.println("MAX JAVA: " + diffJava.getMax());
    System.out.println("MEAN JAVA: " + diffJava.getMean());

    System.out.println("Fast: " + tFast);
    System.out.println("Normal: " + tNormal);
    System.out.println("Bounded: " + tBounded);
    System.out.println("Java: " + tJava);
}

From source file:com.javacreed.examples.sc.part2a.Main.java

public static void main(final String[] args) {
    final String xmlFile = "META-INF/spring/app-context.xml";
    try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(xmlFile)) {

        final long start = System.nanoTime();
        final Fibonacci sequence = context.getBean("fibonacci", Fibonacci.class);
        final long fibNumber = sequence.valueAt(5);
        final int executions = sequence.getExecutions();
        final long timeTaken = System.nanoTime() - start;
        System.out.printf("The 5th Fibonacci number is: %d (%,d executions in %,d NS)%n", fibNumber, executions,
                timeTaken);// w  w w .java2 s . c o  m
    }
}

From source file:com.javacreed.examples.sc.part2b.Main.java

public static void main(final String[] args) {
    final String xmlFile = "META-INF/spring/app-context.xml";
    try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(xmlFile)) {

        final long start = System.nanoTime();
        final Fibonacci sequence = context.getBean("fibonacci2", Fibonacci.class);
        final long fibNumber = sequence.valueAt(5, sequence);
        final int executions = sequence.getExecutions();
        final long timeTaken = System.nanoTime() - start;
        System.out.printf("The 5th Fibonacci number is: %d (%,d executions in %,d NS)%n", fibNumber, executions,
                timeTaken);//from   w  ww.j  a v a2 s .  co  m
    }
}