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

**URL:** <https://discourse.julialang.org/t/prefetch-data-in-a-separate-thread-or-why-adding-a-single-print-leads-to-a-2x-performance/60225>\
**Category:** Machine Learning\
**Created:** [April 29, 2021, 7:41am UTC](https://discourse.julialang.org/t/prefetch-data-in-a-separate-thread-or-why-adding-a-single-print-leads-to-a-2x-performance/60225 "2021-04-29T07:41:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [May 1, 2021, 1:00am UTC](https://discourse.julialang.org/t/prefetch-data-in-a-separate-thread-or-why-adding-a-single-print-leads-to-a-2x-performance/60225/2 "2021-05-01T01:00:10Z")

</div>

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

```julia
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](https://discourse.julialang.org/t/pattern-for-managing-thread-local-storage/54023/2).

---

_[View the full topic](https://discourse.julialang.org/t/prefetch-data-in-a-separate-thread-or-why-adding-a-single-print-leads-to-a-2x-performance/60225)._
