推荐引擎极慢

时间:2019-05-07 16:29:18

标签: laravel performance execution-time

我正在尝试在laravel中为我的网站实现一个推荐引擎。我为它编写了代码,但是代码是 EXTREMELY SLOW 。我运行了 5mins < / em> 直到现在,它仍在加载。我的数据库表都没有多达20行。

有什么方法可以加快它,还是应该只使用laravel / php作为推荐引擎?如果可以的话,用什么语言等等。

这是我的代码(重要部分)

public function showReccPosts(){





    /*
    $tsil =total similarity INdex for likers(those who liked a post)

    */
    $db='laravellikecomment_likes';


      $allposts= DB::table('posts')->pluck('id');
      $allposts = $allposts->toArray();

      $tn=count($allposts);
      $ai=0;$user=Auth::id();

      while ($allposts[$ai] <= $tn) {

         $recclist=array();
         $wl=DB::table($db)->where('item_id',$allposts[$ai])->where('vote', 1)->pluck('user_id');
         $wd=DB::table($db)->where('item_id',$allposts[$ai])->where('vote', -1)->pluck('user_id');
         $nl=count($wl);
         $nd=count($wd);
         $i=0;

        $tsil=0;$tsid=0;
         foreach ($wl as $key => $value) {
             $tsil=si($key,$user);
         }




         foreach ($wd as $key => $value) {
            $tsid=si($key,$user);
        }

            $possibility=$tsil+$tsid;
            $possibility=$possibility*3;
            $tagpossibility= 1;
            $posibility=$possibility+$tagpossibility;
            $posibility=$posibility/4;


            //$recclist = array_combine($allposts, $recclist);
            //arsort($reccclist);



      }







      function si($a,$b){
        $a_like=DB::table($db)->pluck('user_id')->where('user_id',$a)->where('vote',1);
        $a_like=$a_like->toArray();
        $a_dislike=DB::table($db)->pluck('user_id')->where('user_id',$a)->where('vote',-1);
        $a_dislike=$a_dislike->toArray();

        $b_like=DB::table($db)->pluck('user_id')->where('user_id',$b)->where('vote',1);
        $b_like=$a_like->toArray();
        $b_dislike=DB::table($db)->pluck('user_id')->where('user_id',$b)->where('vote',-1);
        $b_dislike=$a_dislike->toArray();

        $c = 0;
          foreach ($a_like as $i) {
           if (in_array($i,$b_like)) $c++;
           }
        $sil= $c/count($a_like);

        $d=0;
        foreach ($a_dislike as $i) {
            if (in_array($i,$b_dislike)) $d++;
            }
        $sid=$d/count($a_dislike);
        $tsil=($sil+$sid)/2;

        return $tsil;
    }




      return view('reccomended.index',compact('allposts','possibility'));

    }

1 个答案:

答案 0 :(得分:0)

我不小心将php脚本发送到一个无限循环中,而忘记了添加$ai++行以增加while循环的偏移量$ai

while ($allposts[$ai] <= $tn)