C语言使用技巧(三十):计算程序运行时间以及自带的函数库calcElapsed(startTime, now())

demo实现:

///* 测量2个事件持续的时间 */  
#include  <stdio.h>
#include  <stdlib.h>
#include  <time.h>  

// // #include<ctime>
// /*测试排序算法运行时间  c++*/
// void testSort(void(*sort)(int[], int), int arr[], int length)
// {
//     clock_t startTime = clock();
//     sort(arr, length);
//     clock_t endTime = clock();
//     // printf(double(endTime - startTime) / CLOCKS_PER_SEC,"s")
// }
// /*插入排序*/
// void insertionSort(int arr[], int n)
// {
//     for (int i = 1; i < n; i++)
//     {
//         int e = arr[i];
//         int j;
//         for (j = i; j >0 && e < arr[j - 1]; j--)
//             arr[j] = arr[j - 1];
//         arr[j] = e;
//     }

// }

//c语言实现
int  main(  void  ) 
{ 
    // /*    程序1:  调用函数计算程序时间            */
    // int arrSize = 10000;
    // int *arr = new int[arrSize];
    // for (int i = 0; i < arrSize; i++)
    // {
    //     arr[i] = rand() % arrSize;
    // }
    // testSort(insertionSort, arr, arrSize);//另一个程序


    /*    程序2:  主函数内循环事件的程序运行时间       */

    long  i  =   10000000L ; //表明这个数是long int类型。
    clock_t start, end; 
    double  duration; 
     /*  测量2个事件持续的时间 */  
    printf(  " Time to do %ld empty loops is  " , i ); 
    
    start  =  clock(); //函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数
    while ( i --  ) ;                   //第srand(time(NULL));
    end  =  clock(); 

    duration  =  ( double )(end  -  start)  /  CLOCKS_PER_SEC; 
    //CLOCKS_PER_SEC表示一秒钟内CPU运行的时钟周期数(时钟计时单元
    printf(  " %f seconds\n " , duration ); 
    // system( " pause " ); 
    return 0;
} 

执行结果:

gcc readtxt.c -o readtxt && ./readtxt
 Time to do 10000000 empty loops is   0.003350 seconds



time.h头文件:


#include <stdint.h>

#if   defined(__APPLE__)
# include <mach/mach_time.h>
#elif defined(_WIN32)
# define WIN32_LEAN_AND_MEAN

# include <windows.h>

#else // __linux

# include <time.h>

# ifndef  CLOCK_MONOTONIC //_RAW
#  define CLOCK_MONOTONIC CLOCK_REALTIME
# endif
#endif

static
uint64_t nanotimer() {
    static int ever = 0;
#if defined(__APPLE__)
    static mach_timebase_info_data_t frequency;
    if (!ever) {
        if (mach_timebase_info(&frequency) != KERN_SUCCESS) {
            return 0;
        }
        ever = 1;
    }
    return  (mach_absolute_time() * frequency.numer / frequency.denom);
#elif defined(_WIN32)
    static LARGE_INTEGER frequency;
    if (!ever) {
        QueryPerformanceFrequency(&frequency);
        ever = 1;
    }
    LARGE_INTEGER t;
    QueryPerformanceCounter(&t);
    return (t.QuadPart * (uint64_t) 1e9) / frequency.QuadPart;
#else // __linux
    struct timespec t;
    if (!ever) {
        if (clock_gettime(CLOCK_MONOTONIC, &t) != 0) {
            return 0;
        }
        ever = 1;
    }
    clock_gettime(CLOCK_MONOTONIC, &t);
    return (t.tv_sec * (uint64_t) 1e9) + t.tv_nsec;
#endif
}


static double now() {
    static uint64_t epoch = 0;
    if (!epoch) {
        epoch = nanotimer();
    }
    return (nanotimer() - epoch) / 1e9;
};

double calcElapsed(double start, double end) {
    double took = -start;
    return took + end;
}

时间计算函数为:

double calcElapsed(double start, double end) {
    double took = -start;
    return took + end;
}

使用方法:

double startTime = now();//当前时间
nsProcess(inBuffer, sampleRate, inSampleCount, channels, kModerate);
double time_interval = calcElapsed(startTime, now());//结束计算时间的位置

参考:https://m.php.cn/manual/view/34332.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值