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 1 year has passed since last update.

[flutter] 「return」と「arrow構文(=>)」でのパフォーマンス検証してみた。

Posted at

はじめに

こんにちは。
今回の記事では、returnを使った関数と、arrow構文を使用した関数でパフォーマンス的な違いがあるのか気になったので実際に検証してみた結果を共有します。

経緯

とあるエンジニアさんと談笑していた際に、
arrow構文とreturnでは、パフォーマンスに違いがあるらしいということを聞いて、
実際に気になったので検証することにしました。

検証方法

String arrowLogic() {
  return "test";
}

String returnLogic () => "test";

上記のString型を返り値に指定した関数を用意します。
その関数をprintで出力するロジックをfor文で1000回試行します。
その前後で現在時刻をms単位で取得して、
その差分を1000回のロジック試行にかかった時間と仮定します。

  GestureDetector(
    onTap: () {
      final startTime = DateTime.now().millisecondsSinceEpoch;
      for(int i = 0; i<1000; i++) {
         print(allowLogic());
           }
          final endTime = DateTime.now().millisecondsSinceEpoch;
          final executionTime = endTime - startTime;
          print("アロー構文ロジックの実行にかかった時間: $executionTime ミリ秒");
        },
                
    child: Text("アロー構文")),

returnの方も同様のロジックを作成します。

全コードはこちらから確認できます。
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              SizedBox(height: 200,),
              GestureDetector(
                onTap: () {
                  final startTime = DateTime.now().millisecondsSinceEpoch;
                  for(int i = 0; i<1000; i++) {
                  print(allowLogic());
                  }
                  final endTime = DateTime.now().millisecondsSinceEpoch;
                  final executionTime = endTime - startTime;
                  print("アロー構文ロジックの実行にかかった時間: $executionTime ミリ秒");


                  final startTime2 = DateTime.now().millisecondsSinceEpoch;
                  for(int i = 0; i<1000; i++) {
                   print(returnLogic());
                  }
                  final endTime2 = DateTime.now().millisecondsSinceEpoch;
                  final executionTime2 = endTime2 - startTime2;
                  print("リターン構文ロジックの実行にかかった時間: $executionTime2 ミリ秒");
                },
                  child: Text("実行")),


            ],
          ),
        ),
      ),
    );
  }
}

String allowLogic() {
  return "test";
}

String returnLogic () => "test";

結果

上記の結果を、50回試行しました。

50回試行した結果の平均値は以下のようになりました。

50回の平均値 arrow構文(ms) return(ms)
116.94 118.42

arrow構文の方が時間が掛かった回数は18回、
returnの方が時間が掛かった回数は32回となりました。

全試行結果はこちらから確認できます。 | n回目 | arrow構文(ms) | return(ms) | |:-----------|------------:|:------------:| | 1 | 117 | 115 | | 2 | 112 | 120 | | 3 | 126 | 115 | | 4 | 136 | 119 | | 5 | 109 | 114 | 6 | 119 | 112 | 7 | 101 | 123 | 8 | 123 | 102 | 9 | 112 | 121 | 10 | 109 | 113 | 11 | 116 | 121 | 12 | 119 | 99 | 13 | 116 |121 | 14 | 121 |110 | 15 | 117 |121 | 16 | 119 |121 | 17 | 114 |115 | 18 | 120 |120 | 19 | 108 |99 | 20 | 116 |123 | 21 | 124 |127 | 22 | 120 |117 | 23 | 117 |116 | 24 | 112 |124 | 25 | 111 |121 | 26 | 114 |118 | 27 | 115 |124 | 28 | 116 |123 | 29 | 131 |107 | 30 | 119 |128 | 31 | 121 |118 | 32 | 117 |112 | 33 | 119 |107 | 34 | 115 |117 | 35 | 116 |124 | 36 |113 |128 | 37 |118 |114 | 38 |121 |117 | 39 |133 |117 | 40 | 125 |119 | 41 |127 |149 | 42 | 112 |122 | 43 | 119 |121 | 44 | 121 |117 | 45 | 112 |119 | 46 | 120 |117 | 47 | 120 |123 | 48 | 122 |128 | 49 | 118 |128 | 50 | 116 |105

考察

上記の結果からだとarrow構文を使用した方が、
微妙にパフォーマンスが良いという結果になりましたが、
returnの方が速い結果も少なくありませんし、
今回はサンプルも少ないので今回の結果では、
誤差の範囲でどっちのパフォーマンスが優れているなどと断言するのは難しそうです。

googleの公式ドキュメントなどにも特に記載はなく、
stackOverFlowなどにも特にパフォーマンスについての言及はありませんでしたし、
冷静に考えてみたら、内部的な処理も変わらなそうですしね。。

結論

時間の無駄でした。
あの話なんだったんだろう。。

最後に

最後までご覧いただきありがとうございました。

何かここら辺に関して知見をお持ちの方がいれば、
ご指摘やご意見いただければありがたいです!

1
0
0

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?