简体   繁体   English

性能/可读性:嵌套for循环与HashMap

[英]Performance / readibility: Nested for-loops vs HashMap

(Note: Sorry if I put this question on the wrong stack exchange, I'll repost it if this question should be going somewhere else...) (注意:很抱歉,如果我把这个问题放在错误的堆栈交换上,如果这个问题应该放在其他地方,我会重新发布它。)

I'm just starting out my first internship at a tech company, and wanted to ask about code performance and/or coding practices. 我刚开始在一家科技公司实习,想问一下代码性能和/或编码实践。 I'm going through code written by a senior dev that doesn't seem right to me in terms of performance, but I'm not sure if it's because I'm inexperienced or if it's something of his. 我正在查看由一位高级开发人员编写的代码,该代码在性能方面对我来说似乎不正确,但是我不确定这是因为我没有经验还是归因于他。

Here's the code that I'm looking at: 这是我正在查看的代码:

// Given the following:
List<TypeA> aList = (...)
List<TypeB> bList = (...)

for(TypeA obj : aList) {
    boolean found = false;

    for(TypeB obj2 : bList) {
        if(obj.name.equals(obj2.name) {
            found = true;
            break;
        }
    }

    if(!found) {
        obj.doSomething();
        someOtherList.add(obj);
    }
}

My thoughts are that a O(n^2) nested for-loop is pretty inefficient for what the code is trying to do. 我的想法是,对于代码尝试执行的操作,O(n ^ 2)嵌套的for循环效率很低。 Would it be better to do something like this? 做这样的事会更好吗? (Also please don't mind any syntax errors, I'm typing this on the fly ;)): (也请不要介意任何语法错误,我正在即时输入它;)):

// Given the following:
List<TypeA> aList = (...)
List<TypeB> bList = (...)

Map<TypeB, String> bListToName = new HashMap<>()
bList.forEach(obj -> bListToName.put(obj, obj.name));

for(TypeA obj : aList) {
    if(bListToName.get(obj.name) == null) {
        obj.doSomething();
        someOtherList.add(obj);
    }
}

My reasoning is that instead of a nested for-loop, I use two O(n) loops instead, which should improve performance by a decent margin, especially if our a/bLists are sufficiently large or used often enough. 我的理由是,我使用了两个O(n)循环,而不是嵌套的for循环,这将以相当大的幅度提高性能,尤其是在我们的a / bList足够大或经常使用的情况下。

Any insight or thoughts would be greatly appreciated, thanks! 任何见解或想法将不胜感激,谢谢!

As you hinted, size is a factor. 如您所暗示,大小是一个因素。 Building the hashmap means allocating additional memory, which could outweigh the time saved on a small number of comparisons. 构建哈希图意味着分配额外的内存,这可能会超过少量比较所节省的时间。

I suggest you get used to doing time tests to put your theories into provable results. 我建议您习惯进行时间测试以使您的理论得到可证明的结果。 You will need to do that anyway to justify such changes during peer review. 无论如何,您都需要这样做以证明在同行评审期间进行此类更改是合理的。

Beyond that, I would just point out that what you are proposing is a half-measure. 除此之外,我只想指出您的建议是一个半措施。 If the code is really critical then it would make sense to structure it as a Map in the first place. 如果代码确实很关键,那么首先将其构造为Map就是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM