简体   繁体   English

是否可以使嵌套的for循环小于O(N ^ 2)?

[英]Is it possible to make this nested for loop less than O(N^2)?

I am working on a Kafka Streaming service using Apache Spark 2.1 and Java 8. I use a nested for loop to populate an ArrayList with Topic/Partition pairs. 我正在使用Apache Spark 2.1和Java 8进行Kafka Streaming服务。我使用嵌套的for循环用Topic / Partition对填充ArrayList

Is it possible to reduce this nested for loop from O(N^2) using another methodology? 是否可以使用另一种方法从O(N ^ 2)减少此嵌套的for循环?

Here is the code: 这是代码:

    private static List<TopicAndPartition> createTAndPList(List<String> topics, List<String> partitions)
        throws ConsumerException {
    if (topics.size() != partitions.size()) {
        throw new ConsumerException("Cannot create list with unequal number of topics and parititons,");
    }

    List<TopicAndPartition> topicsAndPartitions = new ArrayList<>();
    for (int t = 0; t < topics.size(); t++) {
        for (int p = 0; p < Integer.parseInt(partitions.get(t)); p++) {
            topicsAndPartitions.add(new TopicAndPartition(topics.get(t), p));
        }
    }

    return topicsAndPartitions;
}

FYI: I am prevented from using above Kafka 8 due to powers beyond my control (management.) 仅供参考:由于无法控制的能力(管理),我无法在Kafka 8上使用。

With your given code, it does not look possible to reduce the order. 使用您给定的代码,似乎无法减少顺序。

However, there may be two small optimizations you can make. 但是,您可以进行两个小的优化。

  • Move topics.get(t) out of the inner for loop, topic.get(t)从内部for循环中移出,

  • Do not recalculate the inner for loop termination condition every loop. 不要在每个循环中重新计算内部for循环终止条件。

     for (int t = 0; t < topics.size(); t++) { String topic = topics.get(t); int count = Integer.parseInt(partitions.get(t)); for (int p = 0; p < count; p++) { topicsAndPartitions.add(new TopicAndPartition(topic, p)); 

You are calling topics.get and Integer.parseInt(partitions.get(t)) t*p times instead of just t times. 您正在调用topic.getInteger.parseInt(partitions.get(t)) t * p次,而不是t次。 The change of topics.get() will probably not do much, but moving something out of the inner loop like this is a pretty common optimization. 对topic.get()的更改可能不会做很多事情,但是像这样将某些内容移出内部循环是很常见的优化。

Finally, do you really need them all in a list? 最后,您是否真的需要列表中的所有内容? Or can you generate them dynamically where you actually need them? 还是可以在实际需要它们的地方动态生成它们?

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

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