#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <time.h>
typedef unsigned int uint32;
uint32 a;
int main()
{
struct timespec start;
if((clock_gettime( CLOCK_REALTIME, &start)) == -1 )
{
perror("clock gettime\n");
}
/* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt will last while it's processing the background task.*/
//micro seconds output
a = (1000000 * start.tv_sec + start.tv_nsec / 1000);
printf( "%u\n", a);
return EXIT_SUCCESS;
}
我创建了一个计时器来获取任何地点的时间戳,所以上面是一个自由运行的计时器来获取时间戳。我试图以微秒为单位得到输出并得到值:2847675807这是对的?我应该以微秒为单位获得该值。我认为,获得一些更大的价值观。有人请帮帮我。
typedef unsigned int uint64;
typedef unsigned int uint32;
uint32 a;
uint64 timestamp()
{
struct timespec start;
if((clock_gettime( CLOCK_REALTIME, &start)) == -1 )
{
perror("clock gettime\n");
}
/* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt
* will last while it's processing the background task.*/
//micro seconds output
a = (uint32)(1e6 * start.tv_sec + start.tv_nsec * 1e-3);
printf( "%u\n", a);
return EXIT_SUCCESS;
}
int main()
{
timestamp();
return 1;
}
我像上面那样进行了修改但是后面也有相同的结果,比如更大的数字
答案 0 :(得分:0)
也许你可以试试这个。它不是实时的,它不如gettimeofday准确。
#include <time.h>
#include <stdio.h>
int main(int ac, char **av)
{
clock_t start, end;
double duration = 0;
start = clock();
for (int i = 0; i < 100000000; i++)
{}
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f ms\n", duration);
return 0;
}