001 package net.sf.cpsolver.ifs.util; 002 003 /** CPU time measurement. JAVA profiling extension is used. 004 * Java needs to be executed with -Xrunjprof. When the java is executed outside 005 * this profiler, {@link System#currentTimeMillis()} is used. 006 * 007 * @version 008 * IFS 1.1 (Iterative Forward Search)<br> 009 * Copyright (C) 2006 Tomáš Müller<br> 010 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 011 * Lazenska 391, 76314 Zlin, Czech Republic<br> 012 * <br> 013 * This library is free software; you can redistribute it and/or 014 * modify it under the terms of the GNU Lesser General Public 015 * License as published by the Free Software Foundation; either 016 * version 2.1 of the License, or (at your option) any later version. 017 * <br><br> 018 * This library is distributed in the hope that it will be useful, 019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 021 * Lesser General Public License for more details. 022 * <br><br> 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 026 */ 027 public class JProf { 028 private static boolean sPrecise = true; 029 030 /** Current CPU time of this thread in seconds */ 031 public static double currentTimeSec() { 032 return (sPrecise?((double)getCurrentThreadCpuTime())/1e9:((double)System.currentTimeMillis())/1e3); 033 } 034 /** Measurement is based on profiler extension (precise CPU time is returned). 035 * If false, {@link System#currentTimeMillis()} is used in {@link JProf#currentTimeSec()}. 036 */ 037 public static boolean isPrecise() { return sPrecise; } 038 039 /** Current CPU time of this thread (will fail when jprof is not loaded). Use {@link JProf#currentTimeSec()}.*/ 040 public static native long getCurrentThreadCpuTime(); 041 042 static { 043 try { 044 System.loadLibrary("jprof"); 045 if (getCurrentThreadCpuTime()==0l) { 046 int j=0; 047 for (int i=0;i<10000000;i++) j+=i; 048 if (getCurrentThreadCpuTime()==0l) { 049 sPrecise = false; 050 org.apache.log4j.Logger.getLogger(JProf.class).warn("Unable to mesure time in precise -- using System.currentTimeMillis()."); 051 } 052 } 053 } catch (Throwable e) { 054 org.apache.log4j.Logger.getLogger(JProf.class).warn("Unable to mesure time in precise -- using System.currentTimeMillis()."); 055 sPrecise = false; 056 } 057 } 058 }