001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.ui.swingViewer.util; 033 034/** 035 * A simple counter that allows to count the number of frames per second. 036 * 037 * @author Antoine Dutot 038 */ 039public class FpsCounter { 040 // Attribute 041 042 /** 043 * Time measure. 044 */ 045 protected double t1, t2; 046 047 /** 048 * The last frame time. 049 */ 050 protected double time; 051 052 /** 053 * Counter for the average. 054 */ 055 protected int count = 0; 056 057 /** 058 * The average time. 059 */ 060 protected double avgTime; 061 062 // Construction 063 064 public FpsCounter() { 065 } 066 067 // Access 068 069 /** 070 * The number of frames per second according to the last measured frame 071 * (instantaneous measure). 072 * 073 * @return The estimated frame-per-second measure of the last frame. 074 */ 075 public double getFramesPerSecond() { 076 return (1000000000.0 / time); 077 } 078 079 /** 080 * The duration in seconds of the last measured frame. 081 * 082 * @return The last frame time in seconds. 083 */ 084 public double getLastFrameTimeInSeconds() { 085 return (time / 1000000000.0); 086 } 087 088 /** 089 * The number of frames times used to compute the average frame-per-second 090 * and frame time. This number augments with the measures until a maximum, 091 * where it is reset to 0. 092 * 093 * @return The number of frames measure. 094 */ 095 public int getAverageMeasureCount() { 096 return count; 097 } 098 099 /** 100 * The average frame-per-second measure. 101 * 102 * @return The average number of frames per second. 103 * @see #getAverageMeasureCount() 104 */ 105 public double getAverageFramesPerSecond() { 106 return (1000000000.0 / (avgTime / count)); 107 } 108 109 /** 110 * The average frame time. 111 * 112 * @return The time used by a frame in average. 113 */ 114 public double getAverageFrameTimeInSeconds() { 115 return ((avgTime / count) * 1000000000.0); 116 } 117 118 // Command 119 120 public void resetAverages() { 121 count = 0; 122 avgTime = 0; 123 } 124 125 /** 126 * Start a frame measure. 127 */ 128 public void beginFrame() { 129 t1 = System.nanoTime(); 130 } 131 132 /** 133 * End a frame measure. 134 */ 135 public void endFrame() { 136 if (count > 1000000) { 137 count = 0; 138 avgTime = 0; 139 } 140 141 t2 = System.nanoTime(); 142 time = (t2 - t1); 143 avgTime += time; 144 count += 1; 145 } 146}