• Post Reply Bookmark Topic Watch Topic
  • New Topic

Timing within an app  RSS feed

 
Alex Chiu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have specific code, since this is just in the planning stages, but here is my goal:

I want to display the value of a boolean variable from an array at a specific time since the app was launched. (This is a simple goal, but once it works then I can expand on it to do more than just print text.)

My idea is to have a Class that stores the the boolean variable, as well as the time in milliseconds as an integer.

Instances of that class would then be stored in an ArrayList. I'd have a variable to keep track of which part of the ArrayList we're looking at.

I would then loop and check the current app run time in milliseconds. If that time was >= the time stored in the current position of the ArrayList then it'd output the boolean value in that same position of the ArrayList.

Is there a quicker/better approach to this? I'd like to be as accurate as possible.


One other odd thought (but I don't know if it'd increase accuracy) would be to have my loop calculate how many ms it takes to complete each loop, and then when doing the time comparison it would include that value to determine if it should display the boolean value on this cycle, or if it should wait until the next cycle of the loop (depending on the chance of which one would be the most accurate timing.)


 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two thoughts: instead of creating a separate class, it sounds as if a data structure of choice could be a Map<Integer,Boolean>, like a HashMap, with the key being the timestamp.

And instead of looping, I'd use a java.util.TimerTask and a Timer that fires every N seconds (however frequent you want to measure it).
 
Alex Chiu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Two thoughts: instead of creating a separate class, it sounds as if a data structure of choice could be a Map<Integer,Boolean>, like a HashMap, with the key being the timestamp.

And instead of looping, I'd use a java.util.TimerTask and a Timer that fires every N seconds (however frequent you want to measure it).


Your answer intrigues me. I'll have to look into it later today. The reason I was using a separate class is because I was trying to re-create a struct (which is what I'm familiar using in C.) If I can do the same in Java with Map that'd be awesome. I'll look into how it works and see if it'll work for me.

As for TimerTask, I'll need to research if I can use it within the Processing system (which is what I'm using, based on Java.) If it can deal down to a milisecond level (give or take), and is available to use in Processing, then I think it'd work nicely.

I definitely appreciate your ideas

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!