3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CommentInfoController { private $pdo; public function __construct() { $this->pdo = Database::getInstance('db1'); }   public function updateCommentInfo(array $new_comment_info): array { try { $this->pdo->beginTransaction(); // 更新対象id $comment_id = $new_comment_info['comment_id']; /* ステップ1 現状のコメント情報を取得 */ // comments テーブルのSELECT $commentMapper = new CommentMapper(); $comment = new Comment(); $args = ['comment_id'=>$comment_id]; $now_comment_info = $commentMapper->findCommentInfos($args)[0]; if(!$now_comment_info) { throw new InvalidArgumentException('comment_id:' . $comment_id . 'は存在しません'); } /* var_export($now_comment_info); array ( 'comment_id' => 1, 'content' => 'content1', 'tags' => array ( 0 => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), ), ) */ /* ステップ2 現状のコメント情報($now_comment_info) と 新しいコメント情報($new_comment_info) の 差分を抽出して更新 */ // content がある場合 if(!empty($new_comment_info['content'])) { if ($now_comment_info['content'] !== $new_comment_info['content']) { // comments テーブルのUPDATE $commentMapper = new CommentMapper(); $comment = new Comment(); $comment->id = $comment_id; $comment->content = $new_comment_info['content']; $commentMapper->update($comment); } } // tags がある場合 if(!empty($new_comment_info['tags'])) { // tags の差分を得る $tmp = $this->compareTags($now_comment_info['tags'], $new_comment_info['tags']); $added_tags = $tmp['added']; $removed_tags = $tmp['removed']; /* var_export($tmp); array ( 'removed' => array ( 0 => array ( 'tag_name' => 'aaa', 'tag_kind_id' => 1, ), ), 'added' => array ( 0 => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), ), ) */ // $added_tag がDBになければ $tags にセットし bulkInsert で保存 $tags = []; $tagMapper = new TagMapper(); foreach ($added_tags as $added_tag) { $sth = $tagMapper->find($added_tag); $row = $sth->fetch(); if (!$row) { $tag = new Tag(); $tag->fromArray($added_tag); // 配列を行クラスにセット $tags[] = $tag; // 行クラスを追加 } } if(!empty($tags)) { /* var_export($tags); array ( 0 => \Tag::__set_state(array( 'data' => array ( 'tag_name' => 'bbb', 'tag_kind_id' => 2, ), )), ) */ $tagMapper->bulkInsert($tags); } // commentHasTags のINSERT $commentHasTags = []; foreach ($tags as $data) { $commentHasTag = new CommentHasTag(); $commentHasTag->comment_id = $comment_id; $commentHasTag->tag_id = $data->id; $commentHasTags[] = $commentHasTag; } /* var_export($commentHasTags); array ( 0 => \CommentHasTag::__set_state(array( 'data' => array ( 'comment_id' => 1, 'tag_id' => 54, ), )), ) */ $commentHasTagMapper = new CommentHasTagMapper(); $commentHasTagMapper->bulkInsert($commentHasTags); // $removed_tags のリレーションを削除 $commentHasTagMapper = new CommentHasTagMapper(); // ★こういうときって宣言すんのか? foreach ($removed_tags as $removed_tag) { $sth = $tagMapper->find($removed_tag); $row = $sth->fetch(); $conditions = ['comment_id'=>$comment_id, 'tag_id'=>$row->id]; $commentHasTagMapper->delete($conditions); } } $this->pdo->commit(); return $new_comment_info; } catch (Exception $e) { $this->pdo->rollBack(); log($e->getMessage()); } } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.2.70.0180.00079.33
8.2.60.0140.00579.33
8.2.50.0090.00979.33
8.2.40.0080.00879.33
8.2.30.0170.00079.33
8.2.20.0060.00979.33
8.2.10.0140.00379.33
8.2.00.0150.00379.33
8.1.200.0170.00379.33
8.1.190.0160.00379.33
8.1.180.0160.00079.33
8.1.170.0040.01179.33
8.1.160.0100.00679.33
8.1.150.0120.00679.33
8.1.140.0040.01279.33
8.1.130.0080.00879.33
8.1.120.0080.00879.33
8.1.110.0080.00879.33
8.1.100.0130.00379.33
8.1.90.0120.00379.33
8.1.80.0140.00379.33
8.1.70.0150.00279.33
8.1.60.0140.00379.33
8.1.50.0170.00079.33
8.1.40.0100.00779.33
8.1.30.0140.00379.33
8.1.20.0100.00779.33
8.1.10.0090.00979.33
8.1.00.0140.00379.33
8.0.290.0150.00079.33
8.0.280.0150.00079.33
8.0.270.0150.00079.33
8.0.260.0150.00079.33
8.0.250.0100.00579.33
8.0.240.0130.00379.33
8.0.230.0110.00879.33
8.0.220.0160.00079.33
8.0.210.0110.00479.33
8.0.200.0120.00379.33
8.0.190.0150.00379.33
8.0.180.0140.00679.33
8.0.170.0120.00479.33
8.0.160.0090.00679.33
8.0.150.0170.00079.33
8.0.140.0050.01079.33
8.0.130.0120.00379.33
8.0.120.0100.00679.33
8.0.110.0060.01079.33
8.0.100.0150.00079.33
8.0.90.0110.00479.33
8.0.80.0110.00479.33
8.0.70.0070.00779.33
8.0.60.0060.00979.33
8.0.50.0140.00279.33
8.0.30.0150.00079.33
8.0.20.0070.00779.33
8.0.10.0080.00879.33

preferences:
148.08 ms | 1000 KiB | 7 Q