001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.utilities.statistics;
021    
022    /**
023     * Copyright (C) 2007-2008, Jens Lehmann
024     *
025     * This file is part of DL-Learner.
026     * 
027     * DL-Learner is free software; you can redistribute it and/or modify
028     * it under the terms of the GNU General Public License as published by
029     * the Free Software Foundation; either version 3 of the License, or
030     * (at your option) any later version.
031     *
032     * DL-Learner is distributed in the hope that it will be useful,
033     * but WITHOUT ANY WARRANTY; without even the implied warranty of
034     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
035     * GNU General Public License for more details.
036     *
037     * You should have received a copy of the GNU General Public License
038     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
039     *
040     */
041    import org.apache.log4j.Logger;
042    
043    /**
044     * Class for counting time and outputting it.
045     * 
046     * @author Unknown
047     */
048    public class SimpleClock {
049    
050            private static Logger logger = Logger.getLogger(SimpleClock.class);
051    
052            private long time;
053    
054            public SimpleClock() {
055                    time = System.currentTimeMillis();
056            }
057    
058            /**
059             * prints time needed and resets the clock
060             */
061            public void printAndSet() {
062                    logger.info("needed " + getTime() + " ms");
063                    setTime();
064            }
065    
066            /**
067             * prints time needed and resets the clock
068             * 
069             * @param s
070             *            String for printing
071             */
072            public void printAndSet(String s) {
073                    logger.info(s + " needed " + getTime() + " ms");
074                    setTime();
075            }
076    
077            public String getAndSet(String s) {
078                    String ret = s + " needed " + getTime() + " ms";
079                    setTime();
080                    return ret;
081            }
082    
083            /**
084             * prints time needed
085             * 
086             * @param s
087             *            String for printing
088             */
089            public void print(String s) {
090                    logger.info(s + " needed " + getTime() + " ms");
091            }
092    
093            /**
094             * resets the clock
095             */
096            public void setTime() {
097                    time = System.currentTimeMillis();
098            }
099    
100            /**
101             * resets the clock
102             */
103            public void reset() {
104                    setTime();
105            }
106    
107            /**
108             * returns the needed time up to now in ms
109             * @return
110             */
111            public long getTime() {
112                    long now = System.currentTimeMillis();
113                    return now - time;
114            }
115    
116    }