Package org.rascalmpl.interpreter.utils

Source Code of org.rascalmpl.interpreter.utils.Timing

/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:

*   * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
*   * Arnold Lankamp - Arnold.Lankamp@cwi.nl
*******************************************************************************/
package org.rascalmpl.interpreter.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public final class Timing {
  private long start;
 
  public Timing(){
    super();
  }
 
  public void start() {
    start = getCpuTime();
  }
 
  public long duration() {
    long now = getCpuTime();
    long diff = now - start;
    start = now;
    return diff;
  }
 
  public static long getCpuTime( ) {
      ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
      return bean.isCurrentThreadCpuTimeSupported( ) ?
          bean.getCurrentThreadCpuTime( ) : 0L;
  }
  
  /** Get user time in nanoseconds. */
  public static long getUserTime( ) {
      ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
      return bean.isCurrentThreadCpuTimeSupported( ) ?
          bean.getCurrentThreadUserTime( ) : 0L;
  }

  /** Get system time in nanoseconds. */
  public static long getSystemTime( ) {
      ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
      return bean.isCurrentThreadCpuTimeSupported( ) ?
          (bean.getCurrentThreadCpuTime( ) - bean.getCurrentThreadUserTime( )) : 0L;
  }
}
TOP

Related Classes of org.rascalmpl.interpreter.utils.Timing

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.