• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a timer on a loop  RSS feed

 
Trevor Needham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I'm taking a CS class this semester and have an assignment I can't figure out, even after hours of research on the API.

Here is the code I have written:
package Assignment01;

public class SomethingDescriptive {

public static void main(String[] args) {
int x = 0;
int y = -1;
while (x != y) {
x++;
}
System.out.println("The loop has finished running");
}

}

Just a simple exercise to prove that integers have a limited amount of space, very easy. Here is the tricky part, I'm supposed to time how long it takes for the loop to finish running. How do I do this?

Thanks,
Fatal
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by seeing if there's anything in java.util that would have anything to do with time or something like it.

See where you get with that.

And welcome to Javaranch!
 
Trevor Needham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate, but thats the problem, I've searched through that thoroughly and can only find a timer that DOES an action after a set amount of time or on an interval. I can't seem to find anything that tracks the time of a compiler running a loop.

Thanks,
Fatal
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... let's see....

We'll need to count in milliseconds, firstly.

And here's another hint: if you need to know a quantity of something (time, weight, distance), sometimes you start with a quantity, and end with another quantity and figure out what the difference is, right?

Better clue? Go back to java.util.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to record the current time at the start of the loop and record the current time at the end of the loop. There is a class in java.util that gives you the current time. There is also a method in the System class that gives you the time. That's probably easier to find. And that documentation gives you a pointer to the java.util class.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooh..... I forgot about the system class!

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another hint for the java.util class. It's the name of a fruit :-)
 
Trevor Needham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow I feel really stupid for not seeing that... I just created a 2 longs, one for the start and one for the end which tied into a system.currenttimemilis().... So simple, thanks for all the help guys .

Cheers!
-Fatal
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's your extra credit assignment. Create a class called StopWatch that allows you to start the StopWatch, stop it, and provide the elapsed time.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!