Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合Java性能_Java_Performance_Combinations - Fatal编程技术网

组合Java性能

组合Java性能,java,performance,combinations,Java,Performance,Combinations,我想使用这个函数的可能性很大,比如700整数,但是这个函数执行起来时间太长了。有人有提高绩效的想法吗?谢谢:) 公共静态集合组合(列表groupSize,int k){ Set allCombos=newhashset(); //递归的基本情况 如果(k==0){ //只有一个大小为0的组合,即空团队。 添加(新HashSet()); 返回所有组合; } 如果(k>groupSize.size()){ //不可能有团队规模大于团队规模, //因此,返回所有组合而不加入任何团队。 返回所有组合;

我想使用这个函数的可能性很大,比如700整数,但是这个函数执行起来时间太长了。有人有提高绩效的想法吗?谢谢:)

公共静态集合组合(列表groupSize,int k){
Set allCombos=newhashset();
//递归的基本情况
如果(k==0){
//只有一个大小为0的组合,即空团队。
添加(新HashSet());
返回所有组合;
}
如果(k>groupSize.size()){
//不可能有团队规模大于团队规模,
//因此,返回所有组合而不加入任何团队。
返回所有组合;
}
//创建组的副本,并删除一项。
List groupwithout X=新的数组列表(groupSize);
整数x=groupWithoutX.remove(groupWithoutX.size()-1);
设置combosWithoutX=组合(不带x的组,k);
设置combosWithX=组合(没有x的组,k-1);
for(设置组合:combosWithX){
组合。添加(x);
}
allCombos.addAll(combosWithoutX);
allCombos.addAll(combosWithX);
返回所有组合;
}
其他数据结构。您可以尝试使用
位集
而不是
。如果整数值的范围很宽(负值、较大的间距),请使用
groupSize
中的索引

使用索引而不是整数值还有其他优点:所有作为位的子集都可以在for循环中完成(
biginger
as set)

无数据。或生成所有组合的迭代器(流),以重复应用于处理方法

并发性。 平行性只意味着4/8因子。也许吧


优化算法本身

这组集合最好是一个列表。这极大地改善了添加集合的效果。
并显示算法是否没有错误地创建相同的集合。

您需要在返回值上使用
集合
的哪些功能

如果您只需要其中的一些——也许只是代码>迭代器()/代码>或<代码>包含(…)< /代码>,那么您可以考虑返回<代码> Iterator < /COD>,它计算组合的即时值。


有一种有趣的机制可以生成按字典顺序排列的集合的第n个组合。

您不可能在比
n更快的时间内获得所有组合您是否尝试通过设置断点来分解步骤,以检测哪些操作需要“太长”才能执行?(使用秒表)我看不到Java中的简单解决方案(Scala会有很大帮助)。你可能会发现这篇文章很有趣:
public static Set<Set<Integer>> combinations(List<Integer> groupSize, int k) {

    Set<Set<Integer>> allCombos = new HashSet<Set<Integer>> ();
    // base cases for recursion
    if (k == 0) {
        // There is only one combination of size 0, the empty team.
        allCombos.add(new HashSet<Integer>());
        return allCombos;
    }
    if (k > groupSize.size()) {
        // There can be no teams with size larger than the group size,
        // so return allCombos without putting any teams in it.
        return allCombos;
    }

    // Create a copy of the group with one item removed.
    List<Integer> groupWithoutX = new ArrayList<Integer> (groupSize);
    Integer x = groupWithoutX.remove(groupWithoutX.size() - 1);

    Set<Set<Integer>> combosWithoutX = combinations(groupWithoutX, k);
    Set<Set<Integer>> combosWithX = combinations(groupWithoutX, k - 1);
    for (Set<Integer> combo : combosWithX) {
        combo.add(x);
    }
    allCombos.addAll(combosWithoutX);
    allCombos.addAll(combosWithX);
    return allCombos;
}