gpt4 book ai didi

dart - 如何减少方法时间?在Dart for loop中,如果列表的索引约为700,000,则需要13秒。如何减少 'for Loop'?

转载 作者:行者123 更新时间:2023-12-03 04:53:09 25 4
gpt4 key购买 nike

这是创建与Trotter包的组合并将其添加到列表中的代码。
如果大约有100万个索引,则该方法执行将花费13秒以上。似乎花了太长时间,我正在寻找解决方案。

import 'package:trotter/trotter.dart';

void main() {
comboValue();
}

List<int> _choiceNumbers = List.generate(30, (i) => i);

void comboValue() {
final start = DateTime.now();

List<List> combosList = [];
if (_choiceNumbers.length >= 6) {
final bagOfItems = _choiceNumbers, combos = Combinations(6, bagOfItems);


for (final combo in combos()) {
combosList.add(combo);
}

int totalCombo = combos.length.toInt();
} else {}
print(combosList.length);

final end = DateTime.now();
print('method duration: ${end.difference(start)}');
}

最佳答案

如果使用Dart天文台,则可以看到程序中的大多数CPU时间都用于BigInt操作。

Output from Dart Observatory

如果在pub.dev页面上查看trotter软件包,则可以看到这是一种设计选择:

Since we sometimes can be working with indexes so large they cannot be represented using a 64 bit int, indexing and length arem implemented using BigInt.



并由于以下问题而添加: https://bitbucket.org/ram6ler/dart_trotter/issues/2/use-bigint-for-dart2-compatibility

嗯,这种设计选择的问题在于 BigInt的确会对性能产生重大影响,因为对数字的所有运算都将需要额外的处理,即使对于较小的数字也是如此。

解决方案是要求程序包的开发人员提供某种方式来使用该程序包而不使用 BigInt,或者在将其更改为 int的地方创建自己的fork,因为我认为 BigInt的性能无法得到很大改善。

关于dart - 如何减少方法时间?在Dart for loop中,如果列表的索引约为700,000,则需要13秒。如何减少 'for Loop'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61059347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com