1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

なぞの FutureOr

Posted at

dart:async の の中に、 FutureOr という機能があるのだが、
使い方の解説も見ないでの、試して見た。

import 'dart:async';

main() async {
  {
     int start = new DateTime.now().millisecondsSinceEpoch;
     for(int i=0;i<500;i++) {
       await doubleAsync(i);
     }
     int end = new DateTime.now().millisecondsSinceEpoch;
     print("#A# ${end -start}");
  }

  {
     int start = new DateTime.now().millisecondsSinceEpoch;
     for(int i=0;i<500;i++) {
       FutureOr f = doubleOr(i, m:1);
       if(f is Future) {
         await f;
       } 
     }
     int end = new DateTime.now().millisecondsSinceEpoch;
     print("#B# ${end -start}");
  }
 }

Future<int> doubleAsync(int i) async{
	return i+i;
}

int doubleSync(int i) {
	return i+i;
}

FutureOr<int> doubleOr(int i, {int m:0}) {
  if(m == 0) {
    return doubleAsync(i);
  } else {
    return doubleSync(i);
  }
}

https://dartpad.dartlang.org/ 
で試すと、結構速度が違う。

なんとなく

main() asyncOr {
  {
     int start = new DateTime.now().millisecondsSinceEpoch;
     for(int i=0;i<500;i++) {
       awaitOr doubleOr(i, m:1);
     }
     int end = new DateTime.now().millisecondsSinceEpoch;
     print("#A# ${end -start}");
  }

みたいに、かけたら良いのに.

1
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?