测量函数执行时间

很多时候你需要测量某个函数的执行时间,从而进行优化。

在erlang中,通过timer:tc/3可以很方便的获取某个函数的执行时间:


tc(Module, Function, Arguments) -> {Time, Value}

Types Module = Function = atom()
Arguments = [term()]
Time = integer() in microseconds
Value = term()


返回值中Time表示函数消耗时间,单位为ms。
有些时候,我们需要某个函数的执行多次,从而更准确的获取函数的执行时间,我们可以书写这样的函数:



-module(test_avg).
-compile(export_all).

tc(M, F, A, N) when N > 0 ->
L = test_loop(M, F, A, N, []),
Len = length(L),
LSorted = lists:sort(L),
Min = lists:nth(1, LSorted),
Max = lists:nth(Len, LSorted),
Med = lists:nth(round(Len/2), LSorted),
Avg = round(lists:foldl(fun(X, Sum) ->
X + Sum end,
0,
LSorted)/Len),
io:format("Range:~b - ~b mics~n"
"Median:~b mics ~n"
"Average:~b mics ~n",
[Min, Max, Med, Avg]),
Med.

test_loop(_M, _F, _A, 0, List) ->
List;
test_loop(M, F, A, N, List) ->
{T, _R} = timer:tc(M, F, A),
test_loop(M, F, A, N-1, [T|List]).


这样,我们就可以获取某个函数执行多次的最少消耗时间,最大消耗时间,平均消耗时间了。
实验一下:

> 
test_avg:tc(test_state, test, [], 10).
>Range:68 - 99 mics
Median:76 mics
Average:77 mics
C语言测量函数执行时间有多种方法,以下为你详细介绍: ### 使用`timeb`结构体 `timeb`关键字定义变量可以获得具体的毫秒时间。通过获取函数执行前和执行后的系统时间,相减得到函数执行时间(以毫秒为单位)。示例代码如下: ```c #include <stdio.h> #include <time.h> #include <sys/time.h> /** * 测试执行函数时间 */ void DoWork(void) { int num = 0; for (int i = 0; i < 100000000; ++i) { num++; } } int main() { struct timeb tb; ftime(&tb); long start_time = tb.time * 1000 + tb.millitm; DoWork(); ftime(&tb); long end_time = tb.time * 1000 + tb.millitm; double rs = end_time - start_time; printf("rs=%f\n", rs); return 0; } ``` 这种方法可以精确到毫秒级,适用于对时间精度要求较高的场景[^1]。 ### 使用`time()`函数 `time()`函数可用于测量秒数。通过获取操作前和操作后的当前时间,使用`difftime()`函数计算时间差。示例代码如下: ```c #include <stdio.h> #include <time.h> int main() { // 获取操作前的当前时间 time_t start_time = time(NULL); // 执行一些操作(例如,一个简单的循环) for (long i = 0; i < 1000000000; ++i); // 获取操作后的当前时间 time_t end_time = time(NULL); // 计算时间差 double duration = difftime(end_time, start_time); // 打印时间差 printf("花费的时间: %.2f 秒\n", duration); return 0; } ``` 该方法只能精确到秒,适用于执行时间较长的函数测试[^2]。 ### 使用`clock()`函数 在Windows中,`clock()`函数会返回一个CPU当前时间计数值。在函数执行前后分别调用`clock()`函数,将两次的时间计数值相减,即可得到函数执行所用的时间时间单位默认为毫秒。示例代码如下: ```c #include <stdio.h> #include <time.h> void testFunction() { for (int i = 0; i < 1000000; i++); } int main() { clock_t start = clock(); testFunction(); clock_t end = clock(); double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC * 1000; printf("函数执行时间: %f ms\n", time_taken); return 0; } ``` 这种方法简单易用,但受系统调度影响,可能无法完全真实反映函数的运行时间[^3]。 ### 使用`QueryPerformanceCounter()`函数 对于Windows平台,可使用`QueryPerformanceCounter()`函数结合`QueryPerformanceFrequency()`函数测量函数执行时间。示例代码如下: ```c #include <stdio.h> #include <windows.h> // 通用函数指针类型,接受 void* 参数,返回 void* 类型。 typedef void* (*GenericFunction)(void*); // 函数用于测量代码执行时间并打印结果 void measureExecutionTime(GenericFunction func, void* arg) { LARGE_INTEGER frequency, start, end; double executionTime; // 获取性能计数器的频率 QueryPerformanceFrequency(&frequency); // 获取开始时间 QueryPerformanceCounter(&start); // 执行需要测量函数 func(arg); // 获取结束时间 QueryPerformanceCounter(&end); // 计算执行时间(以毫秒为单位) executionTime = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart * 1000; printf("Execution time: %f ms\n", executionTime); } int test_int() { return 2 * 8; } double test_double() { return 3.14 * 6.18; } int test_arg(int arg) { return 10 * arg; } int main() { int arg = 12; // 测量函数类型: 返回double类型,无参数 measureExecutionTime((GenericFunction)test_double, NULL); // 测量函数类型: 返回int类型,有参数 measureExecutionTime((GenericFunction)test_arg, &arg); return 0; } ``` 此方法精度较高,能更准确地测量函数执行时间,且可通过通用函数指针处理不同类型的函数[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值