MRI ruby 线程和性能
MRI ruby threading and performance
关于 SO 的第一个问题,但我潜伏了很长时间,所以如果我违反了任何规则或发布了垃圾问题,你必须原谅我。
我试图更好地理解线程,因此我决定测试 MRI 并了解它的总体性能。
给定以下代码(和输出),为什么线程操作比非线程变体慢得多?
代码
class Benchmarker
def self.go
puts '----------Benchmark Start----------'
start_t = Time.now
yield
end_t = Time.now
puts "Operation Took: #{end_t - start_t} seconds"
puts '----------Benchmark End------------'
end
end
# using mutex
puts 'Benchmark 1 (threaded, mutex):'
Benchmarker.go do
array = []
mutex = Mutex.new
5000.times.map do
Thread.new do
mutex.synchronize do
1000.times do
array << nil
end
end
end
end.each(&:join)
puts array.size
end
# using threads
puts 'Benchmark 2 (threaded, no mutex):'
Benchmarker.go do
array = []
5000.times.map do
Thread.new do
1000.times do
array << nil
end
end
end.each(&:join)
puts array.size
end
# no threads
puts 'Benchmark 3 (no threads):'
Benchmarker.go do
array = []
5000.times.map do
1000.times do
array << nil
end
end
puts array.size
end
输出
Benchmark 1 (threaded, mutex):
----------Benchmark Start----------
5000000
Operation Took: 3.373886 seconds
----------Benchmark End------------
Benchmark 2 (threaded, no mutex):
----------Benchmark Start----------
5000000
Operation Took: 5.040501 seconds
----------Benchmark End------------
Benchmark 3 (no threads):
----------Benchmark Start----------
5000000
Operation Took: 0.454665 seconds
----------Benchmark End------------
提前致谢。
一旦达到大量线程 (5000),调度程序在线程之间切换的开销就远远超过每个线程实际执行的工作量。通常你最多需要 30-50 个线程。
尝试减少线程数量并按比例增加每个线程的工作量:
20.times.map do
Thread.new do
250000.times do
array << nil
end
end
end.each(&:join)
您应该会看到更多可比较的结果。
请注意,您可能会看到下限 Time(threaded) >= Time(non-threaded)
- 即线程版本的时间不能低于 single-threaded 版本。这是因为 MRI 的 GIL 一次只允许一个线程执行(它们永远不能 运行 并行)。某些 ruby 实现(例如 JRuby)允许并行执行线程。
关于 SO 的第一个问题,但我潜伏了很长时间,所以如果我违反了任何规则或发布了垃圾问题,你必须原谅我。
我试图更好地理解线程,因此我决定测试 MRI 并了解它的总体性能。
给定以下代码(和输出),为什么线程操作比非线程变体慢得多?
代码
class Benchmarker
def self.go
puts '----------Benchmark Start----------'
start_t = Time.now
yield
end_t = Time.now
puts "Operation Took: #{end_t - start_t} seconds"
puts '----------Benchmark End------------'
end
end
# using mutex
puts 'Benchmark 1 (threaded, mutex):'
Benchmarker.go do
array = []
mutex = Mutex.new
5000.times.map do
Thread.new do
mutex.synchronize do
1000.times do
array << nil
end
end
end
end.each(&:join)
puts array.size
end
# using threads
puts 'Benchmark 2 (threaded, no mutex):'
Benchmarker.go do
array = []
5000.times.map do
Thread.new do
1000.times do
array << nil
end
end
end.each(&:join)
puts array.size
end
# no threads
puts 'Benchmark 3 (no threads):'
Benchmarker.go do
array = []
5000.times.map do
1000.times do
array << nil
end
end
puts array.size
end
输出
Benchmark 1 (threaded, mutex):
----------Benchmark Start----------
5000000
Operation Took: 3.373886 seconds
----------Benchmark End------------
Benchmark 2 (threaded, no mutex):
----------Benchmark Start----------
5000000
Operation Took: 5.040501 seconds
----------Benchmark End------------
Benchmark 3 (no threads):
----------Benchmark Start----------
5000000
Operation Took: 0.454665 seconds
----------Benchmark End------------
提前致谢。
一旦达到大量线程 (5000),调度程序在线程之间切换的开销就远远超过每个线程实际执行的工作量。通常你最多需要 30-50 个线程。
尝试减少线程数量并按比例增加每个线程的工作量:
20.times.map do
Thread.new do
250000.times do
array << nil
end
end
end.each(&:join)
您应该会看到更多可比较的结果。
请注意,您可能会看到下限 Time(threaded) >= Time(non-threaded)
- 即线程版本的时间不能低于 single-threaded 版本。这是因为 MRI 的 GIL 一次只允许一个线程执行(它们永远不能 运行 并行)。某些 ruby 实现(例如 JRuby)允许并行执行线程。