Considering that the number can be really large, would it be safe to use unsigned long
?
FWIW, I'm adding up all the microseconds from an apache log, thus the number can get really large.
A microsecond is a unit of time in the International System of Units (SI) equal to one millionth (0.000001 or 10−6 or 1⁄1,000,000) of a second.
1900-01-01-00.00. 00.000000. Last 6 digits are micro seconds.
Time#usec() is a Time class method which returns the number of microseconds for time.
time_t almost always represents seconds. Check out clock_t instead. It often has nanosecond precision, you can get the exact amount with the CLOCKS_PER_SEC macro, and you get a clock_t by calling clock() (instead of time() ).
Given that the maximum value you can store in an unsigned long
on a 32 bits platform is around 4G, these 4G microseconds translate to only 71 minutes.
On a 64 bits platform (beware on Windows long is always 32 bits, even on Windows 64 bits), then you can count microseconds for up to 4G times 71 minutes. This is huge: approximately 5800 centuries.
Answer: if your unsigned long
is 64 bits, it's OK.
The best you can do to store a "really large" number with standard C is a unsigned long long int
:
- maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 264 − 1
That is defined in the C99+ standards. If you're willing/wanting to go above and beyond just what C can do, there's other extensions to look at, the first that comes to mind is the GNU MP Bignum library.
A third alternative that I think you should consider is breaking it down into microseconds and seconds the way gettimeofday()
does with a timeeval
structure. That way you don't have to get into ridiculously sized numbers. You can always do the conversions yourself if you want to view the data in just microseconds.
Depends. As noted on wikipedia, an unsigned long
is guaranteed to be at least 32 bits but I guess that can depend on your machine. With 32 bits you can represent numbers as big as 2^32 = 4294967296. 4294967296 micro seconds is 4294 seconds or a bit over 1 hour.
Do you need more? If you do, consider using an unsigned long long
which is guaranteed to be 64 bits, it is C99 though. Otherwise you could always store the seconds separately in another variable like the timeval struct does it.
you can use unsigned long integer.that size is 0 to 4294967295 bytes
A 32-bit unsigned integer would give you about 1 hour 11 minutes, counting in microseconds:
4,294.967295 seconds
A 64-bit unsigned integer, on the other hand, would give you more than half a million years.
unsigned long integer is the best option to store big size data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With