Tested again. The duration with Thread was 4~5 times longer than main thread. For example, it took 40+ scends in Thread, but it merely spent 10 scends in main thread.
Old Ruby used “green” thread. Newer Ruby (2.0) uses native thread, but still managed by a global lock in Ruby. I haven’t been able to figure out why, but I suspect it related to that Ruby still somewhat manages the threads in some way that cause them to not really work well within SU. If you start a Ruby thread in SU it appear to run only as long as the main thread is pumping messages.
Say that you create a thread that increment a variable, and then have a timer that will thread that variable from the thread - you’ll notice that it appear to only increment while the main thread is busy. Even moving the mouse is enough for it to make the thread process, but with no work on the main thread and it appear to somewhat stall.
th.join forces the main thread to wait until th is done. It’s essentially equivalent to the same code without a thread. It is more commonly used when one is creating several threads and then one joins all of them, or
th = Thread.new {
# do something
}
# more code here
th.join
t1 = Time.now
for n in 1..4999999
x = 999999 * n
end
puts "duration: #{Time.now - t1}\n"
uri = URI 'https://wwbim.com/update/msg?msg=time' + (Time.now - t1).to_s
result = Net::HTTP.get uri
The duration is about 0.83 seconds.
With Thread:
th = Thread.new {
t1 = Time.now
for n in 1..4999999
x = 999999 * n
end
puts "duration: #{Time.now - t1}\n"
uri = URI 'https://wwbim.com/update/msg?msg=time' + (Time.now - t1).to_s
result = Net::HTTP.get uri
}
The duration is about 3 seconds. So the calculation in the sub thread is about 3 times slower than in the main thread.