time - php 的for迴圈是非同步執行的嗎?

來源:互聯網
上載者:User
代碼如下:我在php的控制器裡設定了兩個計時器,想看代碼執行的時間,然後出現的問題是,所有輸出的時間都是相同的,也就是$starttime,$endtime和$cirtime都是相同的。難道整個迴圈體在1毫秒內就執行完畢了嗎?還是我代碼寫的有問題?

public function addHotspot(){

    $starttime=time();    $mHotspot = M("hotspot");    for ($i=1; $i<=10000; $i++) {         $hotspot['scene'] = '1';        $hotspot['name'] = 'test';        $hotspot['ath']="0";        $hotspot['atv']="0";        $hotspot['type']="3";        // echo "zzz";        // $mHotspot->add($hotspot);        $cirtime=time();    }     $endtime=time();    $timestrip=$endtime-$starttime;    $result["result"]=true;    $result["starttime"]=$starttime;    $result["endtime"]=$endtime;    $result["timestrip"]=$timestrip;    $result["cirtime"]=$cirtime;    $this->ajaxReturn (json_encode($result),'JSON');}

回複內容:

代碼如下:我在php的控制器裡設定了兩個計時器,想看代碼執行的時間,然後出現的問題是,所有輸出的時間都是相同的,也就是$starttime,$endtime和$cirtime都是相同的。難道整個迴圈體在1毫秒內就執行完畢了嗎?還是我代碼寫的有問題?

public function addHotspot(){

    $starttime=time();    $mHotspot = M("hotspot");    for ($i=1; $i<=10000; $i++) {         $hotspot['scene'] = '1';        $hotspot['name'] = 'test';        $hotspot['ath']="0";        $hotspot['atv']="0";        $hotspot['type']="3";        // echo "zzz";        // $mHotspot->add($hotspot);        $cirtime=time();    }     $endtime=time();    $timestrip=$endtime-$starttime;    $result["result"]=true;    $result["starttime"]=$starttime;    $result["endtime"]=$endtime;    $result["timestrip"]=$timestrip;    $result["cirtime"]=$cirtime;    $this->ajaxReturn (json_encode($result),'JSON');}

不是
PHP 的 time() 單位是秒,1秒內迴圈一萬次不是很正常麼……要返回毫秒的話請用 microtime()

肯定不是非同步

不曉得你在說什麼
代碼也有問題,那個for迴圈沒有意義啊,不停的賦相同的值

php沒有非同步,而且for在js裡面也不是非同步。ps:js裡最簡單判斷是否非同步只需要在回呼函數裡console.log this如果this是window就證明這一步是非同步

看你這段代碼是迴圈寫資料庫,這個習慣可不好

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.