Jump to content

java: stopwatch, timer, however you want to call it :s


Jefke

Recommended Posts

For my school project that have to be finished tomorrow (dam project partner quick studies so I have to do his part) I have to make sudoku, atm console only, not yet GUI.

 

 

anyway I managed to make a working sudoku that checks for pretty much everything I could think of (valid inputs and so on)

 

 

but here is my problem: It has to track the time in seconds a player uses to solve a sudoku (player can quit and resume afterwards)...

 

 

Now I have no idea how to make such thing, does somebody got some suggestions?

 

 

 

ps. if this is a noob question: well then I hope you are kind enough to help this noob ^^

Link to comment
Share on other sites

Java, I hate that language.

 

Anyway, get the system time at the moments:

1. as soon as the player can enter numbers

2. when completed or quit (and if quit, store it to make be able to sum over it next time)

 

For 1, do

starttime = System.currentTimeMillis();

 

For 2, do

stoptime = System.currentTimeMillis();

 

On save/quit/finish, totalTime = (stoptime-starttime) + oldTotalTime (if there is already a value stored, otherwise 0)

On quit, store totalTime

On finish, display totalTime. I hope you can calculate from milli seconds to seconds/minutes/etc :)

 

 

Good luck with it :)

 

 

small edit: Use longs, I think it's the number of miliseconds since January 1, 1970, which are more milliseconds than fit in ints.

  • Like 1
Link to comment
Share on other sites

Java, I hate that language.

 

Anyway, get the system time at the moments:

1. as soon as the player can enter numbers

2. when completed or quit (and if quit, store it to make be able to sum over it next time)

 

For 1, do

starttime = System.currentTimeMillis();

 

For 2, do

stoptime = System.currentTimeMillis();

 

On save/quit/finish, totalTime = (stoptime-starttime) + oldTotalTime (if there is already a value stored, otherwise 0)

On quit, store totalTime

On finish, display totalTime. I hope you can calculate from milli seconds to seconds/minutes/etc :)

 

 

Good luck with it :)

 

 

small edit: Use longs, I think it's the number of miliseconds since January 1, 1970, which are more milliseconds than fit in ints.

 

ty for the fast response (been busy for hours now to get the project done and only now saw the time thing)

 

 

anyway I'm going to try what you suggested.

 

 

 

EDIT: it's working (dam can't believe it took me only a couple of minutes to add it :o)

 

btw it their a difference in using System.currentTimeMillis and System.nanoTime ? Yeah, I know the first uses milliseconds, the second one nanosecond (or I think this is the case, just judging by name). But is there a performance/accuracy differences between the two.

Edited by DrJoske
Link to comment
Share on other sites

Of course the precision is different. But 1/1000th of a second is sufficient for your purposes. Generally, when I want to measure performance (benchmarks for algorithm testing in my case), I use millisecond precision. If that is not helpful, my input is just too small and I need to increase that by a factor 100 :P But since you're testing human input, actually a second is good enough (for sudoko's that is) and required, but 1/1000th might impress your teacher and then I would round to 1/10th for humans not equal to your teacher.

 

Note that precision and accuracy are different things. nanosecond accuracy is not guaranteed due to fluctuations in the entire system.

 

 

Edit: And note that you should use longs as long as possible (and preferably save the longs), only scale down to floats/doubles at the very latest moment. Floats do not have infinite precision :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.