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

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    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?

    Thanks for your help.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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.
    Last edited by m37h0d; 10-02-2009 at 10:36 AM. Reason: had to double check myself on the non-existence of unsigned floating point types.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you measure time in units of half a second then all the values are integers.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks for the responses, guys. I'm considering using an unsigned short int, just as a learning exercise more than anything.

    Quote Originally Posted by tabstop View Post
    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. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So if I do this, I'll use a macro to get the value from where it's stored, right?
    Or rather a function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. zeroing out default constructor?
    By ExDHaos in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2009, 12:20 PM
  2. C++ compile error, help please
    By ExDHaos in forum C++ Programming
    Replies: 13
    Last Post: 05-14-2009, 02:53 PM
  3. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM