Java中的查找表

发布于 2024-12-20 22:00:23 字数 501 浏览 2 评论 0原文

当我想加速部分代码时,我使用查找表而不是函数。在其他语言中,我使用全局数组来完成这项工作。我预先计算了所有返回值并将它们放入一个数组中。

这是一个函数的示例:

public static int Box(int c) { /* c value range is 0-80 */
    return ((c / 9) / 3) * 3 + ((c % 9) / 3);
}

我可以使用这样的数组:

int Box[] = {0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8}

Java 没有全局数组!

为了让我能够访问这些值,最好的方法是什么?

When I want to speedup part of codes, I use a lookup table instead of a function. In other languages I use global arrays to do this job. I pre-calculated all return values and put them in an array.

Here is an example of a function :

public static int Box(int c) { /* c value range is 0-80 */
    return ((c / 9) / 3) * 3 + ((c % 9) / 3);
}

I could instead use an array like this :

int Box[] = {0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8,6,6,6,7,7,7,8,8,8}

Java doesn't have global arrays!

What would be the best way way to do this so I can access these values?

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

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

发布评论

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

评论(5

怎樣才叫好 2024-12-27 22:00:23

您仍然可以使用全局变量。你只需要将它们放入一个类中即可。

public class PrecalculatedValues{
    public static final int BOX[]={0,0,0,1,1,1,...};
}

您可以稍后在代码中访问 BOX,如下所示:

public static void main(String[] args) {
    System.out.println(PrecalculatedValues.BOX[0]);
}

You can still use global variables. You just need to put them in a class.

public class PrecalculatedValues{
    public static final int BOX[]={0,0,0,1,1,1,...};
}

You can access BOX later in your code like so:

public static void main(String[] args) {
    System.out.println(PrecalculatedValues.BOX[0]);
}
生生不灭 2024-12-27 22:00:23

在 Java 中,全局变量相当于公共静态变量。如果你想让静态变量不可变,那么它也应该声明为final。

另外,Java 中静态变量的命名约定是整个名称大写,并用下划线分隔单词(例如:SOME_BOX)。

使用上面的示例,您可以执行以下操作:

public static final int[] BOX = {0,0,0....};

In Java, the equivalent of globals are public static variables. If you want to make the static variable immutable, then it should also be declared as final.

Also, the naming convention in Java for static variables is to capitalize the entire name, and separate words with underscores (ex: SOME_BOX).

Using your example above, you can do:

public static final int[] BOX = {0,0,0....};
黒涩兲箜 2024-12-27 22:00:23

虽然可以使用查找表,但在这种情况下完全没有必要,因为可以在几个周期内计算方程而无需分支。

当使用数组作为查找表时,由于 ArrayIndexOutOfBounds 检查,分支将变得必要。

Though it is possible to use a lookup table it is completly unnecessary in this case, since the equation can be calculated without branches in a couple of cycles.

When using arrays as a lookup table branches will become necessary because of the ArrayIndexOutOfBounds checks.

靑春怀旧 2024-12-27 22:00:23

我建议将静态数组放入该方法所在的类中,并且仍然从外部代码调用该方法,并仅使用函数内的数组。是的,funcall 会产生一些开销,但我想您不会试图获得 0.001% 的性能? JIT 也可能会简化它。最重要的是,你的界面将是一致的,它看起来像一个正常的功能,而不是一些黑客。如果您试图通过预先计算从昂贵的函数中获得一些性能,您也可以考虑记忆。将此数组隐藏在 funcall 后面的原因是,您可以稍后使用任何缓存或预先计算技术,而不会干扰公共接口。

例如

class A {
    private final static int values[] = {0, 1, 2 ... };

    public int myFun(int x){
        return values[x];
    }
}

I would recommend to put a static array inside a class where that method is located and still call the method from outside code and just use the array from within the function. Yes, you will have some overhead for a funcall but I suppose you're not trying to gain like 0.001% of performance? Also JIT will probably simplify it. Most importantly your interface will be consistent and it will look like a normal function not like some hack. If you're trying to get some performance from a costly function by precalculation you can also consider memoization. The thing with hiding this array behind the funcall is that you can use any technique of caching or precalculation later without disturbing public interface.

E.g.

class A {
    private final static int values[] = {0, 1, 2 ... };

    public int myFun(int x){
        return values[x];
    }
}
拧巴小姐 2024-12-27 22:00:23

对于一个,你可以简化你的功能(/9/3*3和括号是非常不必要的。我知道这是一个例子,但总是寻找简化的方法)

其次,如果你的功能足够简单,“加速”方法不一定更快。一般来说,您的查找表方法并不完全可扩展,

第三,如果您仍然需要查找表方法,只需创建一个保存值的对象即可。

for one you could just simplify your function (the /9/3*3 and the parens are very unnecessary. I know this is an example but just always look for ways of simplifying)

secondly if you function is simple enough, the "spedup" methods are not necessarily faster. Generally your lookup table method is not exactly scalable

third if you still want a lookup table method, just create an object that holds the values.

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