什么是最好的方法(性能方面)来测试一个值是否落在阈值内? [英] What is the best way (performance-wise) to test whether a value falls within a threshold?

查看:205
本文介绍了什么是最好的方法(性能方面)来测试一个值是否落在阈值内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,测试最快的方法是什么。

  if(a> =(b- c> ;0≤b-c:0)& 
a< =(b + c< 255b + c:255))

...

如果a,b和c均为 unsigned char aka BYTE 。我试图优化图像扫描过程来找到一个子图像,这样的比较,每次扫描大约300万次,所以即使轻微的优化也是有帮助的。



不知道,但也许是某种按位操作?也许添加1到c和测试小于和大于没有or-equal-to部分?我不知道!

解决方案

好吧,首先让我们看看你正在试图检查什么,下溢检查:

  a> = b  -  c 
a< = b + c
来自两个:
a - b> = -c
a - b <= c

现在等于

  abs(a  -  b)< = c 

$>


$ b a> b?ab:ba)<= c

现在,不包含(或需要)复杂的下溢/溢出检查。






我已经剖析了我和6502的代码1000000000并且正式地没有什么区别。我建议选择最优雅的解决方案(这是IMO矿山,但意见不同),因为性能不是一个争论。






但是,我和asker的代码之间有一个显着的区别。这是我使用的分析代码:

  #include< iostream> 

int main(int argc,char * argv []){
bool prevent_opti;
for(int ai = 0; ai <256; ++ ai){
for(int bi = 0; bi <256; ++ bi){
for = 0; ci <256; ++ ci){
unsigned char a = ai;
unsigned char b = bi;
unsigned char c = ci;
if((a> b?a-b:b-a)<= c)prevent_opti = true;
}
}
}

std :: cout< prevent_opti<< \\\
;

return 0;
}

对于我的if语句,这平均需要120ms,asker的if语句需要135ms平均。


That is, what is the fastest way to do the test

if( a >= ( b - c > 0 ? b - c : 0 ) &&
    a <= ( b + c < 255 ? b + c : 255 ) )

    ...

if a, b, and c are all unsigned char aka BYTE. I am trying to optimize an image scanning process to find a sub-image, and a comparison such as this is done about 3 million times per scan, so even minor optimizations could be helpful.

Not sure, but maybe some sort of bitwise operation? Maybe adding 1 to c and testing for less-than and greater-than without the or-equal-to part? I don't know!

解决方案

Well, first of all let's see what you are trying to check without all kinds of over/underflow checks:

a >= b - c
a <= b + c
subtract b from both:
a - b >= -c
a - b <= c

Now that is equal to

abs(a - b) <= c

And in code:

(a>b ? a-b : b-a) <= c

Now, this code is a tad faster and doesn't contain (or need) complicated underflow/overflow checks.


I've profiled mine and 6502's code with 1000000000 repitions and there officially was no difference whatsoever. I would suggest to pick the most elegant solution (which is IMO mine, but opinions differ), since performance is not an argument.


However, there was a notable difference between my and the asker's code. This is the profiling code I used:

#include <iostream>

int main(int argc, char *argv[]) {  
    bool prevent_opti;
    for (int ai = 0; ai < 256; ++ai) {
        for (int bi = 0; bi < 256; ++bi) {
            for (int ci = 0; ci < 256; ++ci) {
                unsigned char a = ai;
                unsigned char b = bi;
                unsigned char c = ci;
                if ((a>b ? a-b : b-a) <= c) prevent_opti = true;
            }
        }
    }

    std::cout << prevent_opti << "\n";

    return 0;
}

With my if statement this took 120ms on average and the asker's if statement took 135ms on average.

这篇关于什么是最好的方法(性能方面)来测试一个值是否落在阈值内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆