java.lang.System.nanoTime()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(319)

本文整理了Java中java.lang.System.nanoTime()方法的一些代码示例,展示了System.nanoTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.nanoTime()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:nanoTime

System.nanoTime介绍

[英]Returns the current timestamp of the most precise timer available on the local system, in nanoseconds. Equivalent to Linux's CLOCK_MONOTONIC.

This timestamp should only be used to measure a duration by comparing it against another timestamp on the same device. Values returned by this method do not have a defined correspondence to wall clock times; the zero value is typically whenever the device last booted. Use #currentTimeMillis if you want to know what time it is.
[中]返回本地系统上可用的最精确计时器的当前时间戳(以纳秒为单位)。相当于Linux的时钟单调。
此时间戳只能通过与同一设备上的另一个时间戳进行比较来测量持续时间。此方法返回的值与挂钟时间没有定义的对应关系;零值通常在设备上次启动时出现。如果您想知道现在几点,请使用#currentTimeMillis。

代码示例

代码示例来源:origin: google/guava

/** Calls {@link System#nanoTime()}. */
@SuppressWarnings("GoodTime") // reading system time without TimeSource
static long systemNanoTime() {
 return System.nanoTime();
}

代码示例来源:origin: google/guava

/**
 * Returns System.nanoTime() unless the timeout has already elapsed. Returns 0L if and only if the
 * timeout has already elapsed.
 */
private static long initNanoTime(long timeoutNanos) {
 if (timeoutNanos <= 0L) {
  return 0L;
 } else {
  long startTime = System.nanoTime();
  return (startTime == 0L) ? 1L : startTime;
 }
}

代码示例来源:origin: google/guava

/**
 * Returns the remaining nanos until the given timeout, or 0L if the timeout has already elapsed.
 * Caller must have previously sanitized timeoutNanos using toSafeNanos.
 */
private static long remainingNanos(long startTime, long timeoutNanos) {
 // assert timeoutNanos == 0L || startTime != 0L;
 // TODO : NOT CORRECT, BUT TESTS PASS ANYWAYS!
 // if (true) return timeoutNanos;
 // ONLY 2 TESTS FAIL IF WE DO:
 // if (true) return 0;
 return (timeoutNanos <= 0L) ? 0L : timeoutNanos - (System.nanoTime() - startTime);
}

代码示例来源:origin: stackoverflow.com

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime);  //divide by 1000000 to get milliseconds.

代码示例来源:origin: ReactiveX/RxJava

long now() {
  return System.nanoTime();
}

代码示例来源:origin: square/okhttp

private void printEvent(String name) {
 long nowNanos = System.nanoTime();
 if (name.equals("callStart")) {
  callStartNanos = nowNanos;
 }
 long elapsedNanos = nowNanos - callStartNanos;
 System.out.printf("%.3f %s%n", elapsedNanos / 1000000000d, name);
}

代码示例来源:origin: square/okhttp

@Override public void onOpen(WebSocket webSocket, Response response) {
 System.out.println("Executing test case " + number + "/" + count);
 startNanos.set(System.nanoTime());
}

代码示例来源:origin: square/okhttp

@Override public Response intercept(Chain chain) throws IOException {
  long t1 = System.nanoTime();
  Request request = chain.request();
  logger.info(String.format("Sending request %s on %s%n%s",
    request.url(), chain.connection(), request.headers()));
  Response response = chain.proceed(request);
  long t2 = System.nanoTime();
  logger.info(String.format("Received response for %s in %.1fms%n%s",
    request.url(), (t2 - t1) / 1e6d, response.headers()));
  return response;
 }
}

代码示例来源:origin: square/okhttp

private void logWithTime(String message) {
 long timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
 logger.log("[" + timeMs + " ms] " + message);
}

代码示例来源:origin: square/okhttp

@Override public EventListener create(Call call) {
  long callId = nextCallId.getAndIncrement();
  System.out.printf("%04d %s%n", callId, call.request().url());
  return new PrintingEventListener(callId, System.nanoTime());
 }
};

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to100() {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % 100);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to(int max) {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % max);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to100() {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % 100);
}

代码示例来源:origin: ReactiveX/RxJava

private static int randomIntFrom0to(int max) {
  // XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
  long x = System.nanoTime();
  x ^= (x << 21);
  x ^= (x >>> 35);
  x ^= (x << 4);
  return Math.abs((int) x % max);
}

代码示例来源:origin: google/guava

/**
 * Returns the number of milliseconds since time given by startNanoTime, which must have been
 * previously returned from a call to {@link System.nanoTime()}.
 */
long millisElapsedSince(long startNanoTime) {
 return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
}

代码示例来源:origin: square/okhttp

@Override
public void callStart(Call call) {
 startNs = System.nanoTime();
 logWithTime("callStart: " + call.request());
}

代码示例来源:origin: google/guava

@Override
public void run() {
 startTime = System.nanoTime();
 try {
  future.get(timeout, unit);
 } catch (Exception e) {
  // nothing
  exception = e;
 } finally {
  timeSpentBlocked = System.nanoTime() - startTime;
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onComplete() {
  System.out.println("onComplete");
  completeTime.set(System.nanoTime());
  completedLatch.countDown();
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onComplete() {
  System.out.println("onComplete");
  completeTime.set(System.nanoTime());
  completedLatch.countDown();
}

代码示例来源:origin: google/guava

protected void runTestProfiled() throws Throwable {
 long t0 = System.nanoTime();
 try {
  super.runTest();
 } finally {
  long elapsedMillis = (System.nanoTime() - t0) / (1000L * 1000L);
  if (elapsedMillis >= profileThreshold)
   System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
 }
}

相关文章