Why concurrency matters in Ruby - Performance Analysis
When Ruby programs run many tasks at once, concurrency helps manage how fast they finish.
We want to see how running tasks together changes the time it takes compared to running them one by one.
Analyze the time complexity of the following Ruby code using threads.
threads = []
10.times do |i|
threads << Thread.new do
sleep(1) # Simulate work
puts "Task #{i} done"
end
end
threads.each(&:join)
This code runs 10 tasks at the same time using threads, each waiting 1 second before finishing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and running 10 threads that each wait 1 second.
- How many times: 10 times, once per thread.
When tasks run one after another, time adds up. With threads, many tasks run together, so total time can stay close to one task's time.
| Input Size (n) | Approx. Time (seconds) |
|---|---|
| 10 | 10 seconds if sequential, about 1 second with threads |
| 100 | 100 seconds if sequential, about 1 second with threads (if system allows) |
| 1000 | 1000 seconds if sequential, about 1 second with threads (limited by system) |
Pattern observation: Running tasks together can keep total time almost the same, not growing with number of tasks.
Time Complexity: O(1) with concurrency
This means running many tasks at once can keep total time roughly constant, instead of growing with more tasks.
[X] Wrong: "Adding more threads always makes the program faster."
[OK] Correct: Too many threads can slow things down because of overhead and system limits, so speed doesn't always improve.
Understanding how concurrency changes time helps you explain real-world program speed and resource use clearly and confidently.
"What if we changed threads to run tasks one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand concurrency's purpose
Concurrency lets a program handle multiple tasks simultaneously instead of one after another.Step 2: Identify the benefit in Ruby
This improves speed and responsiveness, especially when waiting for slow tasks or handling many users.Final Answer:
It allows the program to do multiple tasks at the same time, improving speed. -> Option AQuick Check:
Concurrency = multitasking speedup [OK]
- Thinking concurrency reduces memory usage
- Believing concurrency fixes bugs automatically
- Confusing concurrency with output formatting
Solution
Step 1: Recall Ruby thread syntax
In Ruby, threads are created using Thread.new with a block of code.Step 2: Check each option
Only Thread.new { ... } is valid Ruby syntax to start a thread.Final Answer:
Thread.new { puts 'Hello from thread' } -> Option AQuick Check:
Thread.new starts threads [OK]
- Using incorrect method names like thread.start
- Trying Java-style thread creation syntax
- Confusing Thread.run with Thread.new
thread = Thread.new { 5.times { |i| puts i; sleep 0.1 } }
thread.join
puts 'Done'Solution
Step 1: Understand thread execution
The thread prints numbers 0 to 4 with a short pause between each.Step 2: Effect of thread.join
thread.join waits for the thread to finish before continuing to print 'Done'.Final Answer:
0 1 2 3 4 Done (each number on its own line) -> Option CQuick Check:
Thread prints numbers then main prints Done [OK]
- Thinking 'Done' prints before numbers
- Assuming no output due to thread
- Ignoring thread.join effect
threads = []
i = 0
5.times do
threads << Thread.new do
puts i
end
i += 1
end
threads.each(&:join)Solution
Step 1: Analyze variable capture in threads
The block uses variable i from the loop, which may change before thread runs, causing unexpected output.Step 2: Identify concurrency issue
Each thread may print the same or wrong i value because i is shared and changes quickly.Final Answer:
The variable i is shared and may cause unexpected output. -> Option BQuick Check:
Loop variable shared in threads causes bugs [OK]
- Thinking Thread.new is wrong syntax
- Confusing threads.each(&:join) with threads.join
- Assuming syntax error without checking code
Solution
Step 1: Understand IO-bound tasks and concurrency
Downloading files is IO-bound and benefits from concurrency because waiting for network is slow.Step 2: Choose concurrency method in Ruby
Ruby threads can run IO tasks concurrently, improving speed without extra processes.Final Answer:
Use threads to download files concurrently because IO waits can run in parallel. -> Option DQuick Check:
Threads speed up IO waits in Ruby [OK]
- Thinking Ruby threads never run in parallel
- Ignoring concurrency benefits for IO tasks
- Adding unnecessary sleep calls
Ready to prove it?
Pass the test and this pattern moves unknown
