Ano*_*non 3 c unix linux microbenchmark
我正在阅读MHZ - lmbench的创建者和源代码浏览代码的基准论文解剖.
在BENCH_INNER()宏内部,我有一个疑问:
#define BENCH_INNER(loop_body, enough) { \
static iter_t __iterations = 1; \
int __enough = get_enough(enough); \
iter_t __n; \
double __result = 0.; \
\
while(__result < 0.95 * __enough) { \
start(0); \
for (__n = __iterations; __n > 0; __n--) { \
loop_body; \
} \
__result = stop(0,0); \
if (__result < 0.99 * __enough \
|| __result > 1.2 * __enough) { \
if (__result > 150.) { \
double tmp = __iterations / __result; \
tmp *= 1.1 * __enough; \
__iterations = (iter_t)(tmp + 1); \
} else { \
if (__iterations > (iter_t)1<<27) { \
__result = 0.; \
break; \
} \
__iterations <<= 3; \
} \
} \
} /* while */ \
save_n((uint64)__iterations); settime((uint64)__result); \
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,BENCH_INNER用于自动计算所选定时间隔('足够')的最佳迭代次数.循环执行直到我们继续迭代一段代码'loop_body',这将占用我们选择的定时间隔的至少95%,其范围可以从5ms到1秒.
为简单起见,让我们将'足够'设为10000微秒
在这种情况下代码不应该重新计算__result吗?我错过了一些基本的东西吗
是的,这里有问题,__ result必须设置为零.
我可以在你的代码中看到另一个可能的问题 - 结果是0.99*enough在一个案例中进行比较,而0.95*enough在其他情况下,这是一个非常可疑的错误.我建议你重写这个宏,明确说明"满足"条件并简化逻辑,首先检查良好条件.像这样:
#define SEARCH_EXIT_CASE(__result, __enough) ((__result) > 0.95 * (__enough) && (__result) < 1.2 * (__enough))
#define BENCH_INNER(loop_body, enough) { \
static iter_t __iterations = 1; \
int __enough = get_enough(enough); \
iter_t __n; \
double __result = 0.; \
\
while(!SEARCH_EXIT_CASE(__result, __enough)) { \
start(0); \
for (__n = __iterations; __n > 0; __n--) { \
loop_body; \
} \
__result = stop(0,0); \
/* good result */ \
if (SEARCH_EXIT_CASE(__result, __enough)) { \
break; \
} \
/* failure cases */ \
if (__result > 150.) { \
double tmp = __iterations / __result; \
tmp *= 1.1 * __enough; \
__iterations = (iter_t)(tmp + 1); \
} else { \
if (__iterations > (iter_t)1<<27) { \
__result = 0.; \
break; \
} \
__iterations <<= 3; \
} \
__result = 0.; \
} /* while */ \
save_n((uint64)__iterations); settime((uint64)__result); \
}
Run Code Online (Sandbox Code Playgroud)
此外,我建议定义其他神奇的常量喜欢1<<27, 1.1, 3, 150.0有像有意义的名称MAX_ITER,CORRECTION_RATE,INCREASE_RATE,RESULT_OVERFLOW,等...
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |