# Thread: Storing a Series of Times e.g. 1, 1.5, 2, 2.5 seconds

1. ## Storing a Series of Times e.g. 1, 1.5, 2, 2.5 seconds

Hi,

Say you have a program that stores a bunch of time values, which are in seconds. They can either be to the nearest full second, or can end in .5. So for example, the first few would be: 0, 0.5, 1, 1.5, 2, and so on.

Say you want to remember when a number of events happen. Like if something happens at 4, 4.5, 7, 12, and 15.5 seconds. What would be a good way to store the series of times?

Right now I'm using a std::vector of type <double>. I'm guessing this is probably not the best way to do it because it uses up more memory than it should. I should also note here that I don't need to store negative values, and the maximum time I would need to go upto would be about 2 hours.

I was thinking I could use an unsigned float. Alternately, because I only need to store either a full number or 0.5, maybe an unsigned short int and convert it whenever I use it or something. What do you guys think? Also is std::vector a good choice for the container?

2. there is no such thing as an unsigned float.

yes, float would be a good choice if you're concerned about the extra memory.

if you're really concerned about RAM consumption, you could double the values and store them as shorts, but there's probably no need to optimize your memory consumption to that extent. particularly since you're using vector, which allocates more ram than it actually needs at any one time to avoid extraneous allocation and copy operations.

yes, your choices of float & vector are good imo.

3. If you knew that the number of integral seconds wouldn't exceed a certain number of bits, you could reserve an extra bit that if set to 1 meant "plus 0.5". But yeah, unless memory is just really tight, the extra effort probably isn't worth it.

4. If you know that the possible range is 0, 0.5, 1, ..., 7199.5, 7200, then store 2*x in an unsigned short int and be done with it.

5. If you measure time in units of half a second then all the values are integers.

6. Just keep it as a vector of doubles. If you know that the numbers will never be more than 2^24, then floating point works just as well.

No point in using integers over floats, it makes the numbers hard to work with. Keep it as double is you need more than 24 bits of precision.

7. Thanks for the responses, guys. I'm considering using an unsigned short int, just as a learning exercise more than anything.

Originally Posted by tabstop
If you know that the possible range is 0, 0.5, 1, ..., 7199.5, 7200, then store 2*x in an unsigned short int and be done with it.
So if I do this, I'll use a macro to get the value from where it's stored, right?

8. So if I do this, I'll use a macro to get the value from where it's stored, right?
Or rather a function.

9. I suppose that depends on what you're going to do with the thing. If it's part of a formula, you just tweak the formula a little bit to deal with the units. If you want the value back plain, then an inline function is what you want.

10. I would recommend against tweaking the formula, unless the values are used in exactly one place. Otherwise it becomes likely that you make a mistake.

If it's used in a very large number of places, it may be worthwhile to create a class for it, but if it's used in a few places, a function should be enough.

It would still be less of a hassle to use floats. You're prematurely optimizing here, and it's gonna cost you readability.