Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper data type to store a sum of microseconds in C

Tags:

c

types

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.

like image 650
Pete Darrow Avatar asked Jun 05 '13 15:06

Pete Darrow


People also ask

How much is a microsecond?

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.

How do you timestamp in microseconds?

1900-01-01-00.00. 00.000000. Last 6 digits are micro seconds.

What is USEC in C?

Time#usec() is a Time class method which returns the number of microseconds for time.

Can Time_t store milliseconds?

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() ).


6 Answers

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.

like image 59
Didier Trosset Avatar answered Nov 10 '22 20:11

Didier Trosset


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.

like image 28
Mike Avatar answered Nov 10 '22 18:11

Mike


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.

like image 43
Sebastian Ärleryd Avatar answered Nov 10 '22 20:11

Sebastian Ärleryd


you can use unsigned long integer.that size is 0 to 4294967295 bytes

like image 2
Amit Gupta Avatar answered Nov 10 '22 19:11

Amit Gupta


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.

like image 1
John Avatar answered Nov 10 '22 20:11

John


unsigned long integer is the best option to store big size data.
like image 1
9827085704 Avatar answered Nov 10 '22 20:11

9827085704