Question

I am reading a nano second value and want to store it in a specific variable, so that I wont loss data. Could someone tell me what could be the data type ??

example :

struct timespec ts;
getrawmonotonic(&ts);
end_time = timespec_to_ns(&ts);

what could be the data type for end_time ??

Was it helpful?

Solution 2

The definition of timespec_to_ns looks like this:

/**
 * timespec_to_ns - Convert timespec to nanoseconds
 * @ts:         pointer to the timespec variable to be converted
 *
 * Returns the scalar nanosecond representation of the timespec
 * parameter.
 */
static inline s64 timespec_to_ns(const struct timespec *ts)
{
        return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
}

So you should store it in a 64-bit integer.

OTHER TIPS

In C++, this would be std::chrono::nanoseconds. For example, to find the length of (wall-)time taken to execute some code, you could write:

auto start = std::chrono::system_clock.now();

//do some things
//...

auto end = std::chrono::system_clock.now();
std::chrono::nanoseconds nanoseconds_taken =
    duration_cast<std::chrono::nanoseconds>(end - start);
std::cout << "Took: " << nanoseconds_taken.count() << " nanoseconds\n";

This really depends on the exact specification of timespec_to_ns.

For linux, the type is s64. On your own system, you should be able to determine the type by looking in the header that declares timespec_to_ns.

In the general case (for C++ only), you can use auto to automatically deduce the correct return type:

struct timespec ts;
auto end_time = timespec_to_ns(&ts);

//Convert to milliseconds:
auto millis = end_time/1000000.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top