Prefetch data in a separate thread (or why adding a single print leads to a 2x performance)

My guess is that, since everything in the main loop is I/O (print and sleep), the tasks in iterate are actually often scheduled on the main thread that the main loop is running (which is not doing anything from the scheduler’s perspective). However, it means that sleep(2) can’t finish at the time it wants to finish unless other task yields; e.g., print something.

This is just speculation, but one way to work around this may be to yield often/sometimes in DataIter. For example

function get(A,n)
    for i = 1:n
        A = A*A';
        A = A/maximum(A)
        yield()  # add this
    end
    return A
end

By the way, I think DataIter can be written much more simply if you use Channel. See the first example in Pattern for managing thread local storage? - #2 by tkf.

1 Like