Fibers for concurrency in PHP - Time & Space Complexity
When using fibers for concurrency in PHP, it's important to understand how the program's running time changes as tasks increase.
We want to see how the number of tasks affects the total execution time when fibers switch between them.
Analyze the time complexity of the following PHP code using fibers.
$fiber1 = new Fiber(function() {
for ($i = 0; $i < 5; $i++) {
echo "Fiber 1: $i\n";
Fiber::suspend();
}
});
$fiber2 = new Fiber(function() {
for ($i = 0; $i < 5; $i++) {
echo "Fiber 2: $i\n";
Fiber::suspend();
}
});
$fiber1->start();
$fiber2->start();
while (!$fiber1->isTerminated() || !$fiber2->isTerminated()) {
if (!$fiber1->isTerminated()) $fiber1->resume();
if (!$fiber2->isTerminated()) $fiber2->resume();
}
This code runs two fibers that each print numbers 0 to 4, switching between them after each print.
Look at what repeats in this code:
- Primary operation: Each fiber runs a loop 5 times, printing and suspending.
- How many times: Each fiber resumes 5 times, so total 10 resumes combined.
Imagine increasing the number of fibers or loop counts:
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (fibers or loop count) | About 10 x 5 = 50 resumes |
| 100 | About 100 x 5 = 500 resumes |
| 1000 | About 1000 x 5 = 5000 resumes |
Pattern observation: The total steps grow roughly linearly with the input if only one of fibers or loop counts increases.
Time Complexity: O(n * m)
This means the total work grows by multiplying the number of fibers (n) and the number of steps each fiber runs (m).
[X] Wrong: "Fibers run all tasks at the same time, so time stays the same no matter how many tasks there are."
[OK] Correct: Fibers switch tasks but still run each step one after another, so total time adds up with more tasks.
Understanding how fibers affect time helps you explain concurrency clearly and shows you know how programs handle multiple tasks efficiently.
"What if we changed the code to run fibers in parallel without suspending? How would the time complexity change?"
Practice
Fiber in PHP?Solution
Step 1: Understand Fiber's role
Fibers allow pausing and resuming code blocks, enabling concurrency without threads.Step 2: Compare options
Only To pause and resume code execution for concurrency correctly describes Fibers' purpose; others describe unrelated features.Final Answer:
To pause and resume code execution for concurrency -> Option CQuick Check:
Fibers = pause and resume concurrency [OK]
- Thinking Fibers create real threads
- Assuming Fibers speed up database automatically
- Confusing Fibers with async loops
Solution
Step 1: Recall Fiber methods
The Fiber class usesstart()to begin execution.Step 2: Check other options
Methods likerun(),begin(), andexecute()do not exist for Fiber.Final Answer:
$fiber->start(); -> Option DQuick Check:
Fiber starts with start() method [OK]
- Using run() instead of start()
- Trying to call execute() or begin()
- Confusing Fiber methods with thread methods
$fiber = new Fiber(function(): void {
$value = Fiber::suspend('paused');
echo "Resumed with: $value";
});
echo $fiber->start();
$fiber->resume('data');Solution
Step 1: Analyze Fiber start()
Thestart()runs untilFiber::suspend('paused'), which returns 'paused'. So, echo outputs 'paused'.Step 2: Analyze Fiber resume()
Theresume('data')continues after suspend, echoing "Resumed with: data".Final Answer:
pausedResumed with: data -> Option AQuick Check:
start() returns suspend value, resume() continues [OK]
- Assuming start() returns void
- Thinking resume() returns suspend value
- Mixing order of output strings
$fiber = new Fiber(function() {
echo "Start\n";
Fiber::suspend();
echo "End\n";
});
$fiber->resume();Solution
Step 1: Check Fiber start method usage
Fibers must be started withstart()before callingresume().Step 2: Validate other options
CallingFiber::suspend()without value is allowed; no return type needed; echo is valid inside Fiber.Final Answer:
Fiber must be started with start(), not resume() first -> Option BQuick Check:
start() first, then resume() [OK]
- Calling resume() before start()
- Thinking suspend() needs a value
- Expecting Fiber function to require return type
$fiber1 = new Fiber(function() {
for ($i = 0; $i < 3; $i++) {
echo "A$i ";
Fiber::suspend();
}
});
$fiber2 = new Fiber(function() {
for ($i = 0; $i < 3; $i++) {
echo "B$i ";
Fiber::suspend();
}
});
// Which code below correctly alternates execution?
A) {
$fiber1->start();
$fiber2->start();
$fiber1->resume();
$fiber2->resume();
}
B) {
$fiber1->start();
$fiber1->resume();
$fiber2->start();
$fiber2->resume();
}
C) {
$fiber1->start();
$fiber2->start();
$fiber2->resume();
$fiber1->resume();
}
D) {
$fiber1->start();
$fiber2->start();
$fiber1->resume();
$fiber1->resume();
}Solution
Step 1: Understand Fiber suspension
Each Fiber suspends after printing one message, so to alternate, we must resume them in turn.Step 2: Analyze options for alternating resumes
Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume() starts both fibers, then resumes fiber1 and fiber2 alternately, producing alternating output. Others resume one fiber multiple times or in wrong order.Final Answer:
Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume() -> Option AQuick Check:
Alternate resume calls for concurrency [OK]
- Resuming one fiber multiple times before the other
- Starting fibers but not resuming correctly
- Resuming fibers in wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
