在 Java 中有效地将向量与一系列数字进行比较

发布于 2024-08-04 19:54:10 字数 509 浏览 2 评论 0原文

我有一系列数字,即6、5、7、8、9、1,通过一些数学过程,我最终将通过重复获得这些数字。因此,我想使用一个向量来存储此过程生成的最后六个数字,然后将该向量的内容与上面的一系列数字进行比较。如果它与这一系列数字完全匹配,我将结束程序,或者如果不匹配,则继续数学过程。

我的问题是我如何有效地比较一系列数字和向量。最初,我打算使用一个简单的 if/else 条件来将向量中的每个单独值与其系列中的对应值进行比较(例如,其中 v 是一个充满六个数字的向量,if (v.get(0) == 6 & ;& v.get(1) == 5 ...)),但考虑到在向量等于级数之前这将被评估多次,我开始怀疑这是否是一个相对昂贵的计算与某些替代程序相比。

我的另一个想法是将这一系列数字存储在第二个向量中,然后比较这两个向量。然而,由于对向量和 Java 缺乏经验,我不确定如何去做这件事,以及它如何比上面提到的简单的 if 和 else 子句更有效。

关于如何更有效地比较一堆数字和向量内容之间的比较有什么建议吗?或者,如果不是向量,那么可能是列表或数组?

I have a series of numbers, i.e. 6, 5, 7, 8, 9, 1, which through some mathematical process I will eventually obtain through repetition. For this reason, I want to use a vector to store the last six numbers yielded by this process and then compare the contents of that vector to the series of numbers above. If it identically matches the series of numbers, I will end the program, or if not continue the mathematical procedure.

My question is I how I might go about comparing the series of numbers and the vector efficiently. Originally I was going to use a simple if/else conditional to compare each individual value in the vector with its correspondent in the series(e.g. where v is a Vector filled with six numbers, if (v.get(0) == 6 && v.get(1) == 5 ...)), but considering that this will be evaluated a number of times before the vector equals the series, I'm beginning to wonder if that would be a relatively costly calculation compared to some alternate procedure.

My other idea to do this is to store the series of numbers in a second vector and then compare the two vectors. However, being inexperienced with vectors and Java in general, I'm not sure as to how I might go about doing this and how it might be more efficient than the simple if and else clause mentioned above.

Any advice as to how comparisons between a bunch of numbers and a vector's contents might be done more efficiently? Or if not a vector, than perhaps a list or array instead?

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

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

发布评论

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

评论(5

暮倦 2024-08-11 19:54:10

从您的数字序列中生成哈希值,例如(警告 - 该哈希函数仅用于演示目的):

[N1,N2,N3,N4,N5] -> N1 异或 N2 异或 N3 异或 N4 异或 N5。

然后,首先您只需要检查结果向量和原始向量的哈希值,并且只有当它们匹配时才需要比较每个单独的数字。

Make a hash value out of your sequence of numbers, example (warning - this hash function is only for demonstration purposes):

[N1,N2,N3,N4,N5] -> N1 xor N2 xor N3 xor N4 xor N5.

Then first you only need to check the hash values of your result vector and the original vector, and only if they match need you to compare each individual number.

锦欢 2024-08-11 19:54:10

这里您应该避免使用 Vector,因为它被实现为线程安全的并且为此有一些开销。使用 LinkedList 来实现插入和删除操作的最大性能,使用 ArrayList 来实现随机访问的最大性能。

示例:

void repeatedCompute() {
    List<Integer> triggerList = new ArrayList<Integer>() {{
        add(6);
        add(5);
        add(7);
        add(8);
        add(9);
        add(1);
    }};

    List<Integer> intList = new LinkedList<Integer>();
    boolean found = false;
    while (!found) {
        int nextResult = compute();

        intList.add(0, nextResult);
        if (intList.size() > triggerList.size()) {
            intList.remove(intList.size() - 1);
        }
        if (intList.equals(triggerList)) {
            found = true;
        }
    }
}

int compute() {
    return new Random().nextInt(10);
}

比较列表的优点是在第一个元素不匹配后停止比较,您不必触及所有元素。比较列表相当快!

You should avoid Vector here, because it's implemented to be thread safe and has some overhead for this. Use LinkedList instead for maximum performance of insert and remove operations and ArrayList for maximum performance of random access.

Example:

void repeatedCompute() {
    List<Integer> triggerList = new ArrayList<Integer>() {{
        add(6);
        add(5);
        add(7);
        add(8);
        add(9);
        add(1);
    }};

    List<Integer> intList = new LinkedList<Integer>();
    boolean found = false;
    while (!found) {
        int nextResult = compute();

        intList.add(0, nextResult);
        if (intList.size() > triggerList.size()) {
            intList.remove(intList.size() - 1);
        }
        if (intList.equals(triggerList)) {
            found = true;
        }
    }
}

int compute() {
    return new Random().nextInt(10);
}

Comparing the lists has the advantage that comparison is stopped after the first element mismatch, you don't have to touch all elements. Comparing lists is quite fast!

弄潮 2024-08-11 19:54:10

我猜想,只保留一个向量与目标数字,然后用新生成的数字填充一个新向量,然后迭代它们进行比较,这不会太昂贵。您可能对第一个数字进行比较失败,因此只需进行一次比较即可检测失败。

看来无论您使用哪种方法,您都必须收集六个数字,因此仅比较整数不会太昂贵。

无论您做什么,请测量!

您应该为比较任务生成至少两种算法并比较它们的性能。选择最快的。

然而,也许第一步是将数学过程的运行时间与比较任务进行比较。如果您的数学过程运行时间是比较的 100 倍或更多,您只需选择一些简单的东西即可,不用担心。

I would guess that it wouldn't be too expensive to just keep one vector around with your target numbers in, then populate a new one with your newly generated numbers, then iterate through them comparing. It may be that you're likely to have a failed comparison on the first number, so it only costs you one compare to detect failure.

It seems that you're going to have to collect your six numbers which ever method you use, so just comparing integers won't be too expensive.

Whatever you do, please measure!

You should generate at least two algorithms for your comparison task and compare their performance. Pick the fastest.

However, maybe the first step is to compare the run time of your mathematical process to the comparison task. If your mathematical process runtime is 100 times or more than the comparison, you just have to pick something simple and don't worry.

氛圍 2024-08-11 19:54:10

请注意,&&运算符短路,即仅当左操作数本身不能确定结果时才计算右操作数。因此,比较只会关注那些必要的元素。

然而,我不认为这在任何情况下都会掩盖用于生成比较数字的数学计算所花费的时间。使用jvisualvm来调查时间都花在哪里了。

Note that the && operator short-circuits, i.e. the right operand is only evaluated if the left operand does not determine the result by itself. Hence the comparison will only look at those elements necessary.

I do not think, however, that this will in any case overshadow the time spent on the mathematical calculations used to generate the numbers you compare. Use jvisualvm to investigate where the time is spent.

木緿 2024-08-11 19:54:10

如果您只需要针对一组数字进行验证,那么直接比较这些数字会更有效。计算哈希需要您执行一些算术,这比比较更昂贵。如果您需要针对多组数字进行验证,那么哈希解决方案会更有效。

If you only need to verify that against one set of numbers, then comparing the numbers directly is more efficient. Computing the hash requires you to perform some arithmetic and that is more expensive than comparison. If you need to verify that against multiple sets of numbers than the hash solution would be more efficient.

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