
#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);
}
#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
#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));
}
#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);
#pragma once
#if _USE_LINUX
#include "InterFace/Linux/TimeCal.h"
#elif _USE_CPP
#include "InterFace/Cpp/TimeCal.h"
#endif
#include "InterFace.h"
int main()
{
TimeCal timer;
memset(&timer, 0, sizeof(timer));
int unit = _MICROSECONDS;
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;
}
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)
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})