Created
April 8, 2022 21:47
-
-
Save collindonnell/ab8d105392fd3fe551df4da3c69d33d7 to your computer and use it in GitHub Desktop.
Ruby Async Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "async" | |
# Simple method to measure how long an operation takes. | |
def measure_time | |
start = Time.now | |
yield | |
puts "Duration: #{Time.now - start}" | |
end | |
def perform_sync | |
sleep 1 | |
sleep 1 | |
end | |
def perform_async | |
Async do |task| | |
task.async do | |
sleep 1 | |
end | |
task.async do | |
sleep 1 | |
end | |
end | |
end | |
measure_time do | |
puts "Synchronous..." | |
perform_sync | |
end | |
measure_time do | |
puts "Async..." | |
perform_async | |
end | |
# Synchronous... | |
# Duration: 2.002028 | |
# Async... | |
# Duration: 1.000946 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment