将数字与范围内的数字列表进行比较的最快方法? [英] Fastest way to compare a number to a list of numbers in a range?

查看:45
本文介绍了将数字与范围内的数字列表进行比较的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比这样做性能更快的方法:

Is there a method faster in performance than doing this:

var value = 2432; 

if (value >= 20 && value <= 31) return true;
else if (value >= 45 && value <= 47) return true;
else if (value >= 82 && value <= 86) return true;
...
else if (value >= 1052 && value <= 1065) return true;
else if (value >= 2400 && value <= 2500) return true;

条件语句包含不在模式中的数字.数字上升到65,000.此外,范围是可变的,彼此不相交.范围存储在 SQL 数据库中:

The conditional statements contain numbers that aren't in a pattern. The numbers go up to 65,000. Also the ranges are variable and don't intersect each other. The range is stored in a SQL database:

columns: from, to
rows: [21, 59], [92, 280], etc...

我最初想创建一个查找表,其中包含范围之间的所有数字(例如,[20, 21, 21, 23, ... 31, -> 45, 46 47]).

I was initially thinking of creating a lookup table containing all the numbers between the ranges (e.g., [20, 21, 21, 23, ... 31, -> 45, 46 47]).

有没有更好的方法?

推荐答案

所以如果范围是唯一的并且彼此不相交.检查我认为是下一个算法的最快方法:

So if ranges are unique and don't intersect with each other. The fastest way to check I see to be next algo:

  1. 制作一个 [start, end] 对列表,或者只使用两个单独的列表作为起点和终点.
  2. 对它们进行排序(在 SQL 端可以很容易地完成)
  3. 使用二分查找找出第一个 start 大于您的参考号的值
  4. 上一项提供了您的唯一范围,您应该对照参考号进行检查.

如果排序是在 DB 端完成的,那么这个算法将是 O(logN),足够快.

If sorting is done on DB side, then this algo will be O(logN) that is fast enough.

这篇关于将数字与范围内的数字列表进行比较的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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