APUE course source code
guowenxue
2 days ago 68826376ee5f47783c644c6604f4411ec747cd7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  eclipse.c
 *    Description:  This file is a time eclipse example
 *
 *        Version:  1.0.0(10/15/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/15/2025 05:19:22 PM"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <time.h>
#include <unistd.h>
 
int main()
{
    struct timespec     start, end;
    long                eclipse;
 
    // 获取开始时间(高精度)
    clock_gettime(CLOCK_MONOTONIC, &start);
 
    // 模拟耗时操作
    sleep(1);
 
    // 获取结束时间
    clock_gettime(CLOCK_MONOTONIC, &end);
 
    // 计算耗时(纳秒)
    eclipse = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
    printf("耗时: %.3f 毫秒\n", eclipse / 1e6);
 
    return 0;
}