-5

I have a task to execute a given method X times per second (example 30 times per second). Within that tick method, I should show the current speed (basically display FPS).

I don't even know how to start. Tried googling but didn't really help.

  • That is too broad. Do you have a game loop already? Calling a method X times is something different than showing the "current speed / FPS" - they are basically opposite. – luk2302 Dec 25 '17 at 14:35
  • This is all of the information I have. It literally states "Create a program that executes any method in a given time interval (example 30 times a second). The method should display the current speed of the execution (number of frames per second). – Benjamin Bajić Dec 25 '17 at 14:51
  • The methods you're looking for are [`System.currentTimeMillis()`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#currentTimeMillis--), and [`Thread.sleep(long millis)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#sleep-long-). To complete your **assignment**, it's up to you to figure out how to use these methods. --- http://idownvotedbecau.se/noattempt/ because it's unlikely that you got this assignment without learning about the tools you need to complete it, so perhaps you should (re)read the course material. – Andreas Dec 25 '17 at 15:08
  • I'm voting to close this question as off-topic because "gimme teh codez." – EJoshuaS - Stand with Ukraine Dec 25 '17 at 15:44

1 Answers1

1

To show something like "fps":

public static void main(String[] args) {
    while (true) callMethod();
}

private static long lastTime = System.currentTimeMillis();
public static void callMethod() {
    long now = System.currentTimeMillis();
    long last = lastTime;
    lastTime = now;

    double fps = 1000 / (double)(now - last);
    System.out.println(fps);
}

You might have to add some sleeps, because otherwise the difference between two steps is just too small, the fps will be "Infinity".

The requirement for 30 times a second is something entirely different. As I said in my comment already that is basically the exact opposite of fps since 30 times a second means exactly 30 fps, which means there is no need to calculate the fps because that is the original requirement.

Further note that "30 times a second" in itself is a generally bad requirement for at least two reasons:

  • is 1000 times a second okay? Does that fulfil the requirement
  • is running the method 30 times in the first millisecond and then waiting the remaining 999ms a useful solution to the problem?
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • This is exactly what should have been done. I should have went through the materials better, it is rather an easy solution. Thank you – Benjamin Bajić Dec 25 '17 at 15:39
  • That is not FPS. That’s just the execution time of one “frame.” – VGR Dec 25 '17 at 16:58
  • @VGR of course it is not fps, there aren‘t even frames involved. But this is what I think OP needed, ignoring the slightly off terminology – luk2302 Dec 25 '17 at 17:00