15, జులై 2025, మంగళవారం

let’s try to make the same program faster using Ruby multithreading techniques instead. Multiple threads within a single process have considerably less overhead than a corresponding number of processes since they share address space and memory. With that in mind, let’s revisit our test case, but this time using Ruby’s Thread class:

 threads = []

puts Benchmark.measure{ 100.times do |i| threads << Thread.new do Mailer.deliver do from "eki_#{i}@eqbalq.com" to "jill_#{i}@example.com" subject "Threading and Forking (#{i})" body "Some content" end end end threads.map(&:join) }

This code now yields the following results (again, on a quad-core processor with MRI Ruby 2.0.0p353):

13.710000   0.040000  13.750000 ( 13.740204)
The Global Interpreter Lock is a mechanism used in computer language interpreters to synchronize the execution of threads so that only one thread can execute at a time. An interpreter which uses GIL will always allow exactly one thread and one thread only to execute at a time, even if run on a multi-core processor. Ruby MRI and CPython are two of the most common examples of popular interpreters that have a GIL.




కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి