Dart- Isolates 在处理列表时非常慢
Dart- Isolates are very slow when working with lists
我想从一个字符串中生成一个包含 300 个项目的列表,但是当这个任务被分成 3 个隔离时,每个隔离生成一个包含 100 个项目的列表,它所花费的时间与单个隔离所花费的时间一样长生成包含 300 个项目的列表。
下面的 fetchCoinList() 方法是隔离中 运行 的方法,而 generateList() 方法生成 3 个隔离。
所有这些分离物都并行工作并执行 return 它们各自的列表。
List<CoinData> fetchCoinList(ListConfig listConfig) {
List<CoinData> coinList = [];
for (int index = listConfig.listStart; index < listConfig.listEnd; index++) {
if (jsonDecode(listConfig.listData)['data'][index]['rank'] == null ||
jsonDecode(listConfig.listData)['data'][index]['priceUsd'] == null ||
jsonDecode(listConfig.listData)['data'][index]['changePercent24Hr'] ==
null) continue;
int rank =
int.parse(jsonDecode(listConfig.listData)['data'][index]['rank']);
String name = jsonDecode(listConfig.listData)['data'][index]['name'];
String symbol = jsonDecode(listConfig.listData)['data'][index]['symbol'];
String id = jsonDecode(listConfig.listData)['data'][index]['id'];
double value = double.parse(
jsonDecode(listConfig.listData)['data'][index]['priceUsd']);
double percentChange = double.parse(
jsonDecode(listConfig.listData)['data'][index]['changePercent24Hr']);
String image =
'https://static.coincap.io/assets/icons/${symbol.toLowerCase()}@2x.png';
print(listConfig.listEnd);
coinList.add(CoinData(rank, id, name, symbol, value, percentChange, image));
}
return coinList;
}
Future<List<CoinData>> generateList(String response) async {
print("Inside isolate");
List<CoinData> coinList = [];
List list = [];
for (int i = 0; i < 3; i++) {
ListConfig listConfig = ListConfig(response, i * 100, (i * 100) + 100);
list.add(compute(fetchCoinList, listConfig));
}
for (int i = 0; i < 3; i++) {
coinList = coinList + await list[i];
}
return coinList;
}
有什么方法可以解决此问题?如果需要(不挂起主线程),如何生成 300 或 1000 项的列表?
您的主要问题可能是您在 fetchCoinList
中处理 JSON 解析的方式有点糟糕 运行 jsonDecode()
您想要从中提取的每个字段你的 JSON 字符串。
您应该改为缓存 jsonDecode()
的结果。另外,你还应该缓存一些你经常做的其他操作。
我做了以下示例来说明我将如何重写代码:
List<CoinData> fetchCoinList(ListConfig listConfig) {
final jsonObject = jsonDecode(listConfig.listData) as Map<String, dynamic>;
List<CoinData> coinList = [];
for (int index = listConfig.listStart; index < listConfig.listEnd; index++) {
final dataMap = jsonObject['data'][index] as Map<String, dynamic>;
if (dataMap['rank'] == null ||
dataMap['priceUsd'] == null ||
dataMap['changePercent24Hr'] == null) continue;
print(listConfig.listEnd);
final symbol = dataMap['symbol'] as String;
coinList.add(CoinData(
rank: dataMap['rank'] as int,
name: dataMap['name'] as String,
symbol: symbol,
id: dataMap['id'] as String,
value: double.parse(jsonObject['priceUsd'] as String),
percentChange: double.parse(jsonObject['changePercent24Hr'] as String),
image:
'https://static.coincap.io/assets/icons/${symbol.toLowerCase()}@2x.png'));
}
return coinList;
}
class CoinData {
final int rank;
final String name;
final String symbol;
final String id;
final double value;
final double percentChange;
final String image;
CoinData({
required this.rank,
required this.name,
required this.symbol,
required this.id,
required this.value,
required this.percentChange,
required this.image,
});
}
我想从一个字符串中生成一个包含 300 个项目的列表,但是当这个任务被分成 3 个隔离时,每个隔离生成一个包含 100 个项目的列表,它所花费的时间与单个隔离所花费的时间一样长生成包含 300 个项目的列表。
下面的 fetchCoinList() 方法是隔离中 运行 的方法,而 generateList() 方法生成 3 个隔离。
所有这些分离物都并行工作并执行 return 它们各自的列表。
List<CoinData> fetchCoinList(ListConfig listConfig) {
List<CoinData> coinList = [];
for (int index = listConfig.listStart; index < listConfig.listEnd; index++) {
if (jsonDecode(listConfig.listData)['data'][index]['rank'] == null ||
jsonDecode(listConfig.listData)['data'][index]['priceUsd'] == null ||
jsonDecode(listConfig.listData)['data'][index]['changePercent24Hr'] ==
null) continue;
int rank =
int.parse(jsonDecode(listConfig.listData)['data'][index]['rank']);
String name = jsonDecode(listConfig.listData)['data'][index]['name'];
String symbol = jsonDecode(listConfig.listData)['data'][index]['symbol'];
String id = jsonDecode(listConfig.listData)['data'][index]['id'];
double value = double.parse(
jsonDecode(listConfig.listData)['data'][index]['priceUsd']);
double percentChange = double.parse(
jsonDecode(listConfig.listData)['data'][index]['changePercent24Hr']);
String image =
'https://static.coincap.io/assets/icons/${symbol.toLowerCase()}@2x.png';
print(listConfig.listEnd);
coinList.add(CoinData(rank, id, name, symbol, value, percentChange, image));
}
return coinList;
}
Future<List<CoinData>> generateList(String response) async {
print("Inside isolate");
List<CoinData> coinList = [];
List list = [];
for (int i = 0; i < 3; i++) {
ListConfig listConfig = ListConfig(response, i * 100, (i * 100) + 100);
list.add(compute(fetchCoinList, listConfig));
}
for (int i = 0; i < 3; i++) {
coinList = coinList + await list[i];
}
return coinList;
}
有什么方法可以解决此问题?如果需要(不挂起主线程),如何生成 300 或 1000 项的列表?
您的主要问题可能是您在 fetchCoinList
中处理 JSON 解析的方式有点糟糕 运行 jsonDecode()
您想要从中提取的每个字段你的 JSON 字符串。
您应该改为缓存 jsonDecode()
的结果。另外,你还应该缓存一些你经常做的其他操作。
我做了以下示例来说明我将如何重写代码:
List<CoinData> fetchCoinList(ListConfig listConfig) {
final jsonObject = jsonDecode(listConfig.listData) as Map<String, dynamic>;
List<CoinData> coinList = [];
for (int index = listConfig.listStart; index < listConfig.listEnd; index++) {
final dataMap = jsonObject['data'][index] as Map<String, dynamic>;
if (dataMap['rank'] == null ||
dataMap['priceUsd'] == null ||
dataMap['changePercent24Hr'] == null) continue;
print(listConfig.listEnd);
final symbol = dataMap['symbol'] as String;
coinList.add(CoinData(
rank: dataMap['rank'] as int,
name: dataMap['name'] as String,
symbol: symbol,
id: dataMap['id'] as String,
value: double.parse(jsonObject['priceUsd'] as String),
percentChange: double.parse(jsonObject['changePercent24Hr'] as String),
image:
'https://static.coincap.io/assets/icons/${symbol.toLowerCase()}@2x.png'));
}
return coinList;
}
class CoinData {
final int rank;
final String name;
final String symbol;
final String id;
final double value;
final double percentChange;
final String image;
CoinData({
required this.rank,
required this.name,
required this.symbol,
required this.id,
required this.value,
required this.percentChange,
required this.image,
});
}