![](/img/trans.png)
[英]Is it possible to compare two binary trees in less than O(n log n) time?
[英]Is it possible to make this nested for loop less than O(N^2)?
我正在使用Apache Spark 2.1和Java 8进行Kafka Streaming服务。我使用嵌套的for
循环用Topic / Partition对填充ArrayList
。
是否可以使用另一种方法从O(N ^ 2)减少此嵌套的for
循环?
这是代码:
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;
}
仅供参考:由于无法控制的能力(管理),我无法在Kafka 8上使用。
使用您给定的代码,似乎无法减少顺序。
但是,您可以进行两个小的优化。
将topic.get(t)从内部for循环中移出,
不要在每个循环中重新计算内部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));
您正在调用topic.get和Integer.parseInt(partitions.get(t)) t * p次,而不是t次。 对topic.get()的更改可能不会做很多事情,但是像这样将某些内容移出内部循环是很常见的优化。
最后,您是否真的需要列表中的所有内容? 还是可以在实际需要它们的地方动态生成它们?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.