一、Ubuntu操作系统中,直接在main中测试clock()设置延迟功能
代码描述:直接在main中使用clock()函数设置200ms延迟。
代码输出: 实现了200ms的延迟。
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
unsigned long tick_start, tick_current, tick_start_i;
tick_start = clock();
tick_current = clock();
struct timeval tv;
gettimeofday(&tv, NULL);
long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
printf("current time in milliseconds: %lld, %lld\n", milliseconds, (long long int)CLOCKS_PER_SEC);
while(tick_current - tick_start < (200 * CLOCKS_PER_SEC / 1000))
{
tick_current = clock();
}
gettimeofday(&tv, NULL);
long long milliseconds2 = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
printf("current time in milliseconds: %lld\n", milliseconds2);
printf("eclipsed