优化查找

发布于 2024-09-13 04:48:07 字数 1287 浏览 7 评论 0原文

我有一个用于查找值的数组。我使用前 2 个值来获取 n 行。 例如,所有行的第一列为 2,第二列为 7。 获取这些值的最快(我的意思是微优化)方法是什么? 我现在使用 for 循环来获取值:

 int l = SpreadLookupTable.GetLength(0);

 for (int iCombo = 0; iCombo < l; iCombo++) {                        
      bool o = SpreadLookupTable[iCombo, 0] == perWeek 
      && SpreadLookupTable[iCombo, 1] == workDays;

     if (o) {
         // do stuff
     }
 }

编辑: 它只有大约 60 行。如果我创建 3 个嵌套数组,这样我就可以像 t[2][7] 一样直接使用前 2 列作为索引,然后我只会迭代我真正需要的行。这样会更快吗?

表:

private static int[,] SpreadLookupTable = {
                                                  {2, 7, 1, 0, 0, 1, 0, 0, 0},
                                                  {2, 7, 1, 0, 0, 0, 1, 0, 0},
                                                  {2, 7, 0, 1, 0, 0, 1, 0, 0},                                                                                                        
                                         ...
                                                  {2, 3, 1, 1, 0, 0, 0, 0, 0},
                                                  {2, 3, 1, 0, 1, 0, 0, 0, 0},
                                                  {2, 3, 0, 1, 1, 0, 0, 0, 0}
                                                  };

I have an array that i use to lookup values. I use the first 2 values to get n rows.
for example all rows that have 2 in the first column and 7 in the second.
What is the fastest (i mean micro-optimized) way to get these values?
I now use a for loop to get the values:

 int l = SpreadLookupTable.GetLength(0);

 for (int iCombo = 0; iCombo < l; iCombo++) {                        
      bool o = SpreadLookupTable[iCombo, 0] == perWeek 
      && SpreadLookupTable[iCombo, 1] == workDays;

     if (o) {
         // do stuff
     }
 }

Edit:
It only has some 60 rows. What if i make 3 nested arrays so i could use the first 2 columns as an index directly like t[2][7] and then i would only iterate over the rows i really need. would that be faster?

the table:

private static int[,] SpreadLookupTable = {
                                                  {2, 7, 1, 0, 0, 1, 0, 0, 0},
                                                  {2, 7, 1, 0, 0, 0, 1, 0, 0},
                                                  {2, 7, 0, 1, 0, 0, 1, 0, 0},                                                                                                        
                                         ...
                                                  {2, 3, 1, 1, 0, 0, 0, 0, 0},
                                                  {2, 3, 1, 0, 1, 0, 0, 0, 0},
                                                  {2, 3, 0, 1, 1, 0, 0, 0, 0}
                                                  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2024-09-20 04:48:07

如果表是静态的并且搜索值的组合已知,则可以将两个搜索值组合成一个散列,并使用带有列表的字典将该散列映射到表中。

如果搜索值未知,您可以构建一个(或多个)多级字典并使用相同的技术。

(我与 Neil N 和你的编辑交叉发布,但这基本上是相同总体思想的一个版本:将索引预处理为某种查找结构。字典或列表是否更有效取决于你的特征数据。)

If the table is static and the combination of search values is known, you could combine both search search values into a hash and map that hash into the table using a dictionary with a list.

If the search values are not known, you could build a multi-level dictionary (or dictionaries) and use the same technique.

(I cross-posted with Neil N and your edit, but this is basically a version of the same general idea: pre-process the indices into some kind of lookup structure. Whether a dictionary or list is more efficient depends on the characteristics of your data.)

梦回梦里 2024-09-20 04:48:07

您可以保留符合条件的 iCombo 索引列表,因此在第一次循环所有数据后,所有后续时间,您只需循环索引并跳过比较。

You could keep a list of indexes of iCombo that match the criteria, so after your first loop through ALL data, all subsequent times, you would only have to loop through the indexes, and skip the comparisons.

甜柠檬 2024-09-20 04:48:07

我在同事正在做的一个项目中看到的一个例子是,他采用了多列网格视图并将单元格导出到数组集中。一个新数组用于索引带有“A”的行,另一个新数组用于索引带有“B”的行。然后创建以下数组来索引 Array1 和 Array 2 中索引位置匹配的那些。使用此功能,您可以对这些行进行微观管理以在循环中使用

 if (o) {
         // do stuff
    }

,从而消除处理这些行的所有需要​​,因为它们似乎满足在

     bool o = SpreadLookupTable[iCombo, 0] == perWeek 
  && SpreadLookupTable[iCombo, 1] == workDays;

我们的测试中设置的标准,它可以将应用程序完成的工作时间减少大约 13%,而不是但我们有几千行需要处理,因此 60 行可能会比较小的索引显示出更好的改进。

An example I saw in a project a colleague of mine was doing was he took a multicolumn grid view and exported the cells to an array set. A new array was used to index the rows with "A", and another new array was used to index the rows with "B". Then a following array made to index those where the index positions in Array1 and Array 2 matched. Using this you can then micro manage these rows to use in your

 if (o) {
         // do stuff
    }

loop, therefore taking out all need to process the rows as they appear to fulfil the criteria set in

     bool o = SpreadLookupTable[iCombo, 0] == perWeek 
  && SpreadLookupTable[iCombo, 1] == workDays;

In our tests it reduce work time done by the app by about 13%, not much but we had several thousand rows to work with so 60 rows may show an even better improvement from the smaller size of your indexes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文