In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to get ns nanosecond time in linux user space". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to get ns nanosecond time in linux user space".
I. introduction
When testing the performance of a program, we often need to obtain ns-level accurate time to measure the performance of a program. Here's how to get ns-level time in user space in linux.
Second, user space to obtain ns-level time
Using the clock_gettime function, the function prototype is as follows:
Long sys_clock_gettime (clockid_t which_clock, struct timespec * tp)
1.which_clock parameter interpretation
CLOCK_REALTIME: the real-time time of the system, which changes with the real-time time of the system, that is, starting from 0:0:0 in UTC1970-1-1. If the system time is supposed to be other by the user, the corresponding time will be changed accordingly.
CLOCK_MONOTONIC: timing starts from the moment the system starts, regardless of the change of the system time by the user
CLOCK_PROCESS_CPUTIME_ID: time spent from this process to the current code system CPU
CLOCK_THREAD_CPUTIME_ID: the time it takes from this thread to the current code system CPU
2.struct timespec structure
The code is as follows:
Struct timespec
{
Time_t tv_sec
Long int tv_nsec
}
The sample code for usage is as follows:
The code is as follows:
# include
# include
# include
Int main (void)
{
Struct timespec time_start= {0, 0}, time_end= {0, 0}
Clock_gettime (CLOCK_REALTIME, & time_start)
Printf ("start time% llus,%llu ns\ n", time_start.tv_sec, time_start.tv_nsec)
Clock_gettime (CLOCK_REALTIME, & time_end)
Printf ("endtime% llus,%llu ns\ n", time_end.tv_sec, time_end.tv_nsec)
Printf ("duration:%llus% lluns\ n", time_end.tv_sec-time_start.tv_sec, time_end.tv_nsec-time_start.tv_nsec)
Return 0
}
Compile command:
The code is as follows:
Gcc test.c-o test-lrt
Running result:
The code is as follows:
. / test
Start time 1397395863s,973618673 ns
Endtime 1397395863s,973633297 ns
Duration:0s 14624ns
From the running results, you can see that calling the printf () function requires about 15us at a time.
At this point, I believe you have a deeper understanding of "how to get ns nanosecond time in linux user space". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.