flutter三种循环比较

1 篇文章 0 订阅

for、forEach、map三种遍历

循环数组中的元素并进行一些改变后返回一个新数组

一般的for、forEach、map耗时比较

var list = List();
for (var i = 0; i < 1000000; i++) {
   list.add(i);
}

int forStart = DateTime.now().microsecondsSinceEpoch;
for (var i = 0; i < list.length; i++) {}
int forEnd = DateTime.now().microsecondsSinceEpoch;
print('for = ${forEnd - forStart}');

int forEachStart = DateTime.now().microsecondsSinceEpoch;
list.forEach((element) {});
int forEachEnd = DateTime.now().microsecondsSinceEpoch;
print('forEach = ${forEachEnd - forEachStart}');

int mapStart = DateTime.now().microsecondsSinceEpoch;
list.map((e) {});
int mapEnd = DateTime.now().microsecondsSinceEpoch;
print('map = ${mapEnd - mapStart}');

第一次打印

I/flutter (17551): for = 11926
I/flutter (17551): forEach = 13340
I/flutter (17551): map = 32

第二次打印

I/flutter (17551): for = 11987
I/flutter (17551): forEach = 13067
I/flutter (17551): map = 29

第三次打印

I/flutter (17551): for = 11707
I/flutter (17551): forEach = 12299
I/flutter (17551): map = 27

注: dart中建议不要在函数字面量中使用' forEach '​​​​​​​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值