5

I have a function in Julia that requires to do things in a loop. A parameter is passed to the loop, and the bigger this parameter, the slower the function gets. I would like to have a message to know in which iteration it is, but it seems that Julia waits for the whole function to be finished before printing anything. This is Julia 1.4. That behaviour was not on Julia 1.3. A example would be like this

function f(x)
   rr=0.000:0.0001:x
   aux=0
   for r in rr
      print(r, " ")
      aux+=veryslowfunction(r)
   end
  return aux
end

As it is, f, when called, does not print anything until it has finished.

3 Answers 3

8

You need to add after the print command:

flush(stdout)

Explanation

The standard output of a process is usually buffered. The particular buffer size and behavior will depend on your system setting and perhaps the terminal type. By flushing the buffer you make sure that the contents is actually sent to the terminal.

Sign up to request clarification or add additional context in comments.

3 Comments

I replicated your example and on my machine I do not need to flush. The behavior of your code will differ across operating systems and consoles. But this "strangeness" is not Julia-specific. Is my answer flush(stdout) working for you?
Yeah, it is... but with julia 1.3 was not needed. And not expected.
When you upgraded other things might have changed. For example on Julia 1.4 I can not replicate it. Such things happen when you work with streams - be it pipes or files - always flush if you want to be sure and do not rely on system config.
3

Alternatively, you can also use a library like ProgressLogging.jl (needs TerminalLoggers.jl to see actual output), or ProgressMeter.jl, which will automatically update a nicely formatted status bar during each step of the loop.

For example, with ProgressMeter, a call to

function f(x)
   rr=0.000:0.0001:x
   aux=0
   @showprogress for r in rr
      aux += veryslowfunction(r)
   end
  return aux
end

will show something like (in the end):

Progress: 100%|██████████████████████████████████████████████████████████████| Time: 0:00:10

Comments

0

Again I can't reproduce the behaviour in my terminal (it always prints), but I wanted to add that for these types of situations the @show macro is quite neat:

julia> function f(x)
          rr=0.000:0.0001:x
          aux=0
          for r in rr
             @show r
             aux+=veryslowfunction(r)
          end
         return aux
       end
f (generic function with 1 method)

julia> f(1)
r = 0.0
r = 0.0001
r = 0.0002
...

It uses println under the hood:

julia> using MacroTools

julia> a = 5
5

julia> prettify(@expand(@show a))
quote
    Base.println("a = ", Base.repr($(Expr(:(=), :ibex, :a))))
    ibex
end

1 Comment

println without flushing will be shown or not. However, many caching mechanism implementations might cache line-by-line and hence it has a bigger chance of being executed than print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.