计时接口(Linux/Cpp)

c 复制代码
/* InterFace/Linux/TimeCal.c */
#include "TimeCal.h"

int startTime(TimeCal *timer)
{
    if (clock_gettime(CLOCK_MONOTONIC, &timer->start) != 0)
    {
        perror("clock_gettime");
        return -1;
    }

    return 0;
}

int stopTime(TimeCal *timer)
{
    if (clock_gettime(CLOCK_MONOTONIC, &timer->end) != 0)
    {
        perror("clock_gettime");

        return -1;
    }
    return 0;
}

long getElapsedTime(const TimeCal *timer, int unit)
{
    long seconds = timer->end.tv_sec - timer->start.tv_sec;
    long nanoseconds = timer->end.tv_nsec - timer->start.tv_nsec;

    switch (unit)
    {
    case 1: // 返回秒
        return seconds;
    case 2: // 返回毫秒
        return seconds * 1000 + nanoseconds / 1000000;
    case 3: // 返回微秒
        return seconds * 1000000 + nanoseconds / 1000;
    default:
        return -1; // 无效的单位
    }
}

void timeUsleep(unsigned int time)
{
    usleep(time);
}
c 复制代码
/* InterFace/Linux/TimeCal.h */
#pragma once
#include <stdio.h>
#include <time.h>
#include <unistd.h> 
#include <memory.h> 

#ifdef __cplusplus
extern "C" {
#endif

typedef enum _TIME_TYPE
{
    _SECONDS = 1,
    _MILLISECONDS,
    _MICROSECONDS
}TIME_TYPE;

typedef struct _TimeCal
{
    struct timespec start;
    struct timespec end;
}TimeCal;

extern int startTime(TimeCal *timer);
extern int stopTime(TimeCal *timer);
long getElapsedTime(const TimeCal *timeCal, int unit);
extern void timeUsleep(unsigned int time);

#ifdef __cplusplus
}
#endif
c 复制代码
/* InterFace/Cpp/TimeCal.c */
#include "TimeCal.h"

int startTime(TimeCal *timeCal)
{
    timeCal->start = std::chrono::steady_clock::now();
    return 0;
}

int stopTime(TimeCal *timeCal)
{
    timeCal->end = std::chrono::steady_clock::now();
    return 0;
}

long getElapsedTime(const TimeCal *timeCal, int unit)
{
    using namespace std::chrono;

    auto duration = timeCal->end - timeCal->start;

    switch (unit)
    {
    case 1: // 返回秒
        return duration_cast<seconds>(duration).count();
    case 2: // 返回毫秒
        return duration_cast<milliseconds>(duration).count();
    case 3: // 返回微秒
        return duration_cast<microseconds>(duration).count();
    default:
        return -1; // 无效的单位
    }
}

void timeUsleep(unsigned int time)
{
    std::this_thread::sleep_for(std::chrono::microseconds(time));
}
c 复制代码
/* InterFace/Cpp/TimeCal.h */
#pragma once

#include <stdio.h>
#include <time.h>
#include <memory.h>

#include <iostream>
#include <chrono>
#include <thread>

typedef enum _TIME_TYPE
{
    _SECONDS = 1,
    _MILLISECONDS,
    _MICROSECONDS
}TIME_TYPE;

typedef struct _TimeCal
{
    std::chrono::steady_clock::time_point start;
    std::chrono::steady_clock::time_point end;
} TimeCal;

extern int startTime(TimeCal *timer);
extern int stopTime(TimeCal *timer);
long getElapsedTime(const TimeCal *timeCal, int unit);
extern void timeUsleep(unsigned int time);
c 复制代码
/* InterFace.h */
#pragma once

/* Cmake input */
// #define _USE_LINUX 1
// #if _USE_LINUX
// #define _USE_CPP 0
// #else
// #define _USE_CPP 1
// #endif

#if _USE_LINUX
#include "InterFace/Linux/TimeCal.h"
#elif _USE_CPP
#include "InterFace/Cpp/TimeCal.h"
#endif
c 复制代码
/* main.c */
#include "InterFace.h"

int main()
{
    TimeCal timer;
    memset(&timer, 0, sizeof(timer));

    int unit = _MICROSECONDS; // 选择时间单位:1-秒,2-毫秒,3-微秒

    startTime(&timer);
    timeUsleep(1000000);
    stopTime(&timer);

    long elapsed_time = getElapsedTime(&timer, unit);

    if (elapsed_time >= 0)
    {
        printf("Time taken: %ld %s\n", elapsed_time, (unit == 1) ? "seconds" : ((unit == 2) ? "milliseconds"
                                                                                           : "microseconds"));
    }
    else
    {
        printf("Invalid time unit selected.\n");
    }

    return 0;
}
bash 复制代码
cmake_minimum_required(VERSION 3.5)
project(MyProject)
set(CMAKE_VERBOSE_MAKEFILEON)
add_compile_options(-std=c++11 -Wall)

# 添加条件语句,选择不同路径下的源文件
if (_USE_LINUX)
    add_definitions(-D_USE_LINUX=1)
    file(GLOB SOURCE_FILES main.c InterFace/Linux/*.c)
#    set(SOURCE_FILES main.c InterFace/Linux/TimeCal.c)
#elseif (_USE_CPP)
#    add_definitions(-D_USE_CPP=1)
#    set(SOURCE_FILES main.c InterFace/Cpp/TimeCal.c)
else()
    add_definitions(-D_USE_CPP=1)
    file(GLOB SOURCE_FILES main.c InterFace/Cpp/*.c)
endif()

# 设置可执行文件的输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build)

# 添加源文件
add_executable(MyExecutable ${SOURCE_FILES})

# 添加包含路径
target_include_directories(MyExecutable PRIVATE ${PROJECT_SOURCE_DIR})
相关推荐
Christal_pyy15 分钟前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
csbDD1 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
专注VB编程开发20年3 小时前
除了 EasyXLS,加载和显示.xlsx 格式的excel表格,并支持单元格背景色、边框线颜色和粗细等格式化特性
c++·windows·excel·mfc·xlsx
Natsuagin3 小时前
轻松美化双系统启动界面与同步时间设置(Windows + Ubuntu)
linux·windows·ubuntu·grub
我们的五年4 小时前
【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)
linux·网络·http
夏天的阳光吖4 小时前
C++蓝桥杯基础篇(四)
开发语言·c++·蓝桥杯
oioihoii4 小时前
C++17 中的 std::to_chars 和 std::from_chars:高效且安全的字符串转换工具
开发语言·c++
张胤尘4 小时前
C/C++ | 每日一练 (2)
c语言·c++·面试
強云5 小时前
23种设计模式 - 装饰器模式
c++·设计模式·装饰器模式
yatingliu20195 小时前
代码随想录算法训练营第六天| 242.有效的字母异位词 、349. 两个数组的交集、202. 快乐数 、1. 两数之和
c++·算法