Skip to content

Commit

Permalink
同步与异步区别
Browse files Browse the repository at this point in the history
  • Loading branch information
OMGZui committed Nov 7, 2018
1 parent f894400 commit 661653d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/coroutine/diff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: 小粽子
* Date: 2018/11/7
* Time: 21:48
*/

require __DIR__ . '/../bootstrap.php';

// 协程
$time = microtime(true);
// 创建10个协程
for ($i = 0; $i < 10; ++$i) {
// 创建协程
go(function () use ($i) {
co::sleep(1.0); // 模拟请求接口、读写文件等I/O
dump($i);
});
}
swoole_event_wait();
dump("协程用时: ", microtime(true) - $time);

// 同步
$time = microtime(true);
// 创建10个协程
for ($i = 0; $i < 10; ++$i) {
sleep(1); // 模拟请求接口、读写文件等I/O
dump($i);
}
dump("同步用时: ", microtime(true) - $time);

0 comments on commit 661653d

Please sign in to comment.