[LINUX] Time measurement using a clock

I made a library of time measurement using a clock.

Supported

C language standard

compiler

OS

I don't know the rest

logic

  1. Use __builtin_readcyclecounter () for Clang
  2. Use __rdtsc () with GCC and x86 architecture
  3. For GCC and Linux, use timespec_get ()

I don't know the rest

Source code

clockcycle.h


#ifndef CLOCKCYCLE_H
#define CLOCKCYCLE_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#ifdef __clang__
static inline uint64_t now() {
	return __builtin_readcyclecounter();
}
#elif defined(__GNUC__)
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
#include <x86intrin.h>
static inline uint64_t now() {
	return __rdtsc();
}
#elif defined(__linux__)
#include <time.h>
static inline uint64_t now() {
	struct timespec ts = {0, 0};
	timespec_get(&ts, TIME_UTC);
	return (uint64_t)(ts.tv_sec) * 1000000000 + ts.tv_nsec;
}
#else
#error unsupported architecture
#endif
#endif

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // CLOCKCYCLE_H

in conclusion

Actually, I would like to rewrite the code of the instruction to read the clock for ARM as well, referring to the following.

https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm/40455065

Recommended Posts

Time measurement using a clock
Calculation time measurement using maf
Write processing time measurement a little easier using the with clause
measurement of time
python time measurement
Sample to draw a simple clock using ebiten
Measurement of execution time
Try face detection in real time using a webcam
Pepper Tutorial (5): Using a Tablet
Using a printer with Debian 10
Execution time measurement with Python With
A memorandum of using eigen3
I tried to make a regular expression of "time" using Python
I stopped my instance at a specific time using AWS Lambda
Generate a Docker image using Fabric
A memorandum when using beautiful soup
Creating a web application using Flask ②
I made a Line-bot using Python!
Learn Zundokokiyoshi using a simple RNN
Create a python GUI using tkinter
Drawing a silverstone curve using python
Make a face recognizer using TensorFlow
Create a nested dictionary using defaultdict
Collective measurement of Volume using FSL
Creating a simple table using prettytable
[FSL] Image measurement using ROI (VOI)
Creating a web application using Flask ①
Using a serial console on Ubuntu 20.04
Creating a learning model using MNIST
A story about using Python's reduce
Creating a web application using Flask ③
Create a CRUD API using FastAPI
Face detection using a cascade classifier
Creating a web application using Flask ④
Using a webcam with Raspberry Pi
Create a C wrapper using Boost.Python
python> Processing time measurement> time.time () --start_time
I won't have a hard time anymore. .. Multi-process using Python's standard library.
Try using a Linux server as a backup destination for Time Machine (Ver. 2020)
A useful note when using Python for the first time in a while