Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java get elapsed time

    public int fps;
    private int frames;
    private long fpsTimer;
    public double deltaTime;
    private double lastDeltaTime;
    public boolean calculatePerformance;

    public Thread thread = new Thread() {
      public void run() {
          while(running) {

              // Update, Render, etc.

              if(calculatePerformance) calculatePerformance();
          }
      }
    };

    private final void calculatePerformance() {
        frames++; // Add 1 to frames each update.
        if(System.currentTimeMillis() - fpsTimer >= 1000L) { // If 1 second has passed.
            fpsTimer = System.currentTimeMillis(); // Update timer to current time.
            fps = frames; // Set fps to frames
            frames = 0; // Reset frames
        }
        deltaTime = (System.nanoTime() - lastDeltaTime) / 1_000_000_000.0D; // Set delta time from last delta time
        lastDeltaTime = System.nanoTime(); // Set last deltaTime to current delta Time
    }
 
PREVIOUS NEXT
Tagged: #java #elapsed #time
ADD COMMENT
Topic
Name
5+1 =