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 long remove(Set set) {
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        start = System.nanoTime();
        set.remove(set.size() - 1 - i);//from  ww  w  . j  a va  2  s  .c o m
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:Main.java

public static long getNanoTime() {
    return System.nanoTime();
}

From source file:Main.java

/**
 * Get a nonce for an OAuth request.  OAuth defines the nonce as "a random
 * string, uniquely generated for each request. The nonce allows the Service
 * Provider to verify that a request has never been made before and helps
 * prevent replay attacks when requests are made over a non-secure channel
 * (such as HTTP)."//from  w  ww .j a  v  a  2  s . c o  m
 *
 * @return the nonce string to use in an OAuth request
 */
public static String getNonce() {
    return Long.toString(System.nanoTime());
}

From source file:de.berlios.jhelpdesk.utils.StampUtils.java

public static String craeteStampFromObjects(Object first, Object... rest) {
    StringBuilder sb = new StringBuilder(Thread.currentThread().getName());
    sb.append(Thread.currentThread().getId());
    sb.append(first);/*w  w w .j  a  v  a  2 s .  c  om*/
    for (Object o : rest) {
        sb.append(o);
    }
    sb.append(System.nanoTime());
    return DigestUtils.shaHex(sb.toString());
}

From source file:com.cloudera.MultiplexedTest.java

public static void main(String[] args) throws Exception {
    options = new Options();
    final Configuration conf = new Configuration();
    final FileSystem fs = FileSystem.get(new URI(options.uri), conf);

    long nanoStart = System.nanoTime();
    BaseThread threads[] = new BaseThread[options.nThreads];
    if (options.isWriteOperation) {
        for (int i = 0; i < options.nThreads; i++) {
            threads[i] = WriterThread.create(i, fs);
        }/*  www  .j a  v a2 s .c  om*/
    } else {
        for (int i = 0; i < options.nThreads; i++) {
            threads[i] = ReaderThread.create(i, fs);
        }
    }
    for (int i = 0; i < options.nThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < options.nThreads; i++) {
        threads[i].join();
    }
    for (int i = 0; i < options.nThreads; i++) {
        Throwable t = threads[i].getException();
        if (t != null) {
            System.err.println("there were exceptions.  Aborting.");
            System.exit(1);
        }
    }
    long nanoEnd = System.nanoTime();
    fs.close();
    long totalIo = options.nThreads;
    totalIo *= options.nBytesPerThread;
    float nanoDiff = nanoEnd - nanoStart;
    float seconds = nanoDiff / 1000000000;
    float rate = totalIo / seconds;
    System.out.println(String.format("Using %d threads, average rate was %s/s\n" + "Total time was %f seconds",
            options.nThreads, prettyPrintByteSize(rate), seconds));
}

From source file:Main.java

public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(concurrency);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(concurrency);

    for (int i = 0; i < concurrency; i++) {
        executor.execute(new Runnable() {
            @Override/*from  w  w  w. jav a  2 s.co m*/
            public void run() {
                ready.countDown(); // Tell timer we're ready
                try {
                    start.await(); // Wait till peers are ready
                    action.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    done.countDown(); // Tell timer we're done
                }
            }
        });
    }

    ready.await(); // Wait for all workers to be ready
    long startNanos = System.nanoTime();
    start.countDown(); // And they're off!
    done.await(); // Wait for all workers to finish
    return System.nanoTime() - startNanos;
}

From source file:com.relicum.ipsum.Utils.Profiler.java

public static void startProfiling(String id) {
    final long nanos = System.nanoTime();
    synchronized (startTimes) {
        if (startTimes.containsKey(id)) {
            throw new IllegalStateException("This ID is already being profiled!");
        }//from   w  w w  .j  ava 2 s  .  c o m
        startTimes.put(id, nanos);
    }
}

From source file:com.sanlq.common.IPIP.java

public static IPIP init() {
    Long st = System.nanoTime();
    IPIP ip = new IPIP();
    Long et = System.nanoTime();
    log.info((et - st) / 1000 / 1000);/*from   w  ww  .ja va2  s  . com*/
    return ip;
}

From source file:com.yahoo.parsec.clients.ParsecAsyncProgressTimer.java

/**
 * progress time.//from   w  ww. j  a  va  2s.  c  om
 * @param progress progress data object
 * @param opCode operation code
 */
public static void progressTime(ParsecAsyncProgress progress, TimerOpCode opCode) {
    //
    // time in microsecond
    //
    long now = System.nanoTime() / DateUtils.MILLIS_PER_SECOND;
    switch (opCode) {
    case TIMER_STARTSINGLE:
        progress.setStartSingleTime(now);
        break;
    case TIMER_NAMELOOKUP:
        progress.setNsLookupTime(now - progress.getStartSingleTime());
        break;
    case TIMER_CONNECT:
        progress.setConnectTime(now - progress.getStartSingleTime());
        break;
    case TIMER_STARTTRANSFER:
        progress.setStartTransferTime(now - progress.getStartSingleTime());
        break;
    case TIMER_PRETRANSFER:
        progress.setPreTransferTime(now - progress.getStartSingleTime());
        break;
    case TIMER_TOTAL:
        progress.setTotalTime(now - progress.getStartSingleTime());
        break;
    default:
        LOGGER.warn("opcode=" + opCode + " is not defined");
        break;
    }
}

From source file:io.anserini.search.SearchCollection.java

public static void main(String[] args) throws Exception {
    SearchArgs searchArgs = new SearchArgs();
    CmdLineParser parser = new CmdLineParser(searchArgs, ParserProperties.defaults().withUsageWidth(90));

    try {/* w  w w.  j  a va 2 s.  c  om*/
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        System.err.println("Example: SearchCollection" + parser.printExample(OptionHandlerFilter.REQUIRED));
        return;
    }

    final long start = System.nanoTime();
    SearchCollection searcher = new SearchCollection(searchArgs);
    int numTopics = searcher.runTopics();
    searcher.close();
    final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    LOG.info("Total " + numTopics + " topics searched in "
            + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss"));
}