jjb/threaded-rails-example — Issue #1: Why is it slower?
Author: brutuscat
Created: 2012-01-18 18:27:20+00:00
State: open

I was thinking about this and it seems obvious:

I would say that it is slower because you're doing concurrency via its threads.

Explanation: if Task1 takes 1sec to finish and Task2 takes 2 secs to finish both tasks it will take you 3 secs at leas, t if you do it sequentially. This is one after the other:

T1: |-|-|-| 1sec
T2: |-|-|-|-|-|-| 2sec
DONE in 3secs

If you do it concurrently, this is one bit at a time per each task, you will end up finishing BOTH tasks at the same ~3seconds. Or the shorter one some time AFTER its real time, because you will use some time to do stuff with the other Task. It's like this:

T1: |-| |-| |-| somehow more than 1 sec
T2: |-| |-| |-|-|-|-| somehow more than 2 sec
DONE in 3secs

Ok this is if there is no thread switching via your OS.

Actually what could happen is that if your App is IO bound, the OS will switch to a waiting thread (a thread that it's waiting for a response from the IO for example), this way it will use THAT idle processing time. So it seems like it's processing in parallel with the other tasks, but what it does is to use that idle time for another tasks. Example:
Taking into account tha what its in **bold** does IO:

T1: |-| **|-|** |-| somehow more than 1 sec
T2: |-| |-|-|-|-|-| somehow more than 2 sec but using cpu from the IO task part.
DONE in ~3secs