# Why is printing to a terminal slow?

**URL:** https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987
**Category:** Performance
**Created:** [July 13, 2020, 4:34pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987 "2020-07-13T16:34:18Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 4:34pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/1 "2020-07-13T16:34:19Z")

</div>

Hello,  
I tried just a simple benchmark of printing a million numbers in an array.

Surprisingly, it takes a lot of time. I imagine this might be connected to how julia communicates with the system, printing parts not the whole chunk. Is that the case? How do I make it faster?

I have modified the julia example to show that most of the time is spent printing but similar times occur when using the Python equivalent:

```julia
a = []

function populate()
	global a
	for i = 1:1000000
		push!(a, i)
	end
end

function printit()
	println(a)
end

populate()
@time printit()

```

```julia
$ time julia gen.jl
....
 17.392193 seconds (6.53 M allocations: 213.003 MiB, 0.39% gc time)

real	0m17.653s
user	0m10.038s
sys	0m7.569s

```

```julia
a = []
for i in range(1, 1000001):
	a.append(i)

print(a)

```

```julia
$ time python3 gen.py
....
real	0m0.399s
user	0m0.187s
sys	0m0.051s

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 13, 2020, 4:42pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/2 "2020-07-13T16:42:06Z")

</div>

The python version takes 46 seconds in my terminal. Are you redirecting the output of the Python program to `/dev/null` or something?

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 4:44pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/3 "2020-07-13T16:44:15Z")

</div>

No, I see the numbers in my terminal. I use alacritty terminal. EDIT: I just tried with Urxvt and Uxterm and it made no difference.

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 4:46pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/4 "2020-07-13T16:46:21Z")

</div>

```julia
$ uname -srm
Linux 5.6.16-1-MANJARO x86_64
$ python3 -V
Python 3.8.3
$ julia -v
julia version 1.4.2

```

I run the examples as given above.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 13, 2020, 5:01pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/5 "2020-07-13T17:01:10Z")

</div>

I can reproduce that by installing and using alacritty. There’s a couple of natural follow on questions:

1. what is Python doing to make printing to alacritty this fast?
2. why are you printing that much data to a terminal?

Printing to a file, these are the same general speed:

```julia
python3 print_array.py > out.py.txt 0.19s user 0.03s system 97% cpu 0.230 total
julia print_array.jl > out.jl.txt 0.75s user 0.15s system 134% cpu 0.671 total

```

It would be good to get to the bottom of the first question and see if there’s something Julia can do to be faster in this case, but there’s also a reason it hasn’t come up before: printing a lot of data to a terminal is not generally something one wants to do.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 13, 2020, 5:03pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/6 "2020-07-13T17:03:54Z")

</div>

I can reproduce @asmar’s results using the built-in terminal on Ubuntu 18.04. For `time python3 gen.py` I get:

```julia
real	0m0.882s
user	0m0.181s
sys	0m0.040s

```

and just doing `@time println(collect(1:1000000))` in Julia 1.4.2 gives: `13.133670 seconds (6.13 M allocations: 200.272 MiB, 0.35% gc time)`

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 5:06pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/7 "2020-07-13T17:06:52Z")

</div>

1. I am sorry, I edited my comment previously as I didn’t expect it to have any significance - I have tried with Uxterm and Urxvt instead of alacritty and the performance was the same. I am curious what terminal emulator you, @StefanKarpinski , use? I have no idea as to why it might work well with some terminals and not the others.

2. I am learning julia and was just fiddling around. 😉 However, I feel like this is something worth looking at anyway. Sometimes you may want to just run a script multiple times while debugging and not modify the script not to print the huge data.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 13, 2020, 5:10pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/8 "2020-07-13T17:10:54Z")

</div>

I use iTerm2.

> [@asmar](#):
>
> I feel like this is something worth looking at anyway.

Sure. Definitely good to look into, as I said. (Issue opened: [#36639](https://github.com/JuliaLang/julia/issues/36639))

> Sometimes you may want to just run a script multiple times while debugging and not modify the script not to print the huge data.

But why print huge data to the terminal in the first place? To a file, ok, that makes sense (and is fast). To a terminal, what’s the point? You can’t look at it all since it’s huge and you’re not saving it to a file… so why print it at all?

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [July 13, 2020, 5:11pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/9 "2020-07-13T17:11:06Z")

</div>

There might be some buffering optimization in Python: doing

```julia
for i in range(1, 1000001):
        print(i)

```

instead of printing the array uses 5.5s instead of 0.8s in my terminal (still faster than Julia with 16s though)

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [July 13, 2020, 5:15pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/10 "2020-07-13T17:15:07Z")

</div>

Makes sense given how the first script’s output shows up all at once. Terminal emulators are really not optimized for large quantities of text output…

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 5:17pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/11 "2020-07-13T17:17:35Z")

</div>

> [@StefanKarpinski](#):
>
> I use iTerm2.

I cannot test that, unfortunately.

> [@StefanKarpinski](#):
>
> But why print huge data to the terminal in the first place? To a file, ok, that makes sense (and is fast). To a terminal, what’s the point? You can’t look at it all since it’s huge and you’re not saving it to a file… so why print it at all?

A similar example has happened to me many times. Let’s say I am working with an array of 10 elements regularly. I am printing each item and in the end I print a value calculated in the loop. I realize I have a mistake in my computation algorithm and test with a large input to get a feeling of what might have happened wrong - thus I am only concerned about the final print and just ignore the preceding mess. 🙂 But sure, you’re right that this is not the typical use case, @StefanKarpinski.

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 5:19pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/12 "2020-07-13T17:19:47Z")

</div>

Yes, I have also tried this. This has been my initial inspiration of what might be happening in Julia.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [July 13, 2020, 5:23pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/13 "2020-07-13T17:23:32Z")

</div>

Thanks for bringing this up. If you encounter any other issues, don’t hesitate to post.

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 5:35pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/14 "2020-07-13T17:35:24Z")

</div>

[https://github.com/JuliaLang/julia/issues/36639](https://github.com/JuliaLang/julia/issues/36639)

---

<div class="post-metadata">

### Author: ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)
#### Post date: [July 13, 2020, 6:35pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/15 "2020-07-13T18:35:03Z")

</div>

I want to add, that on windows, this is deadly slow. I actually expected this (before I tried it).  
The script from the first post takes 122 seconds (in `cmd`).  
PowerShell did not seem faster (EDIT: 400s).  
I also tried `wt`: it took ages as well (EDIT: 280s)  
I am not sure what terminal vscode uses by default, but it was definitely the slowest (EDIT: 1414s)

Notably I did run some of these in parallel.  
I wonder if the size of the terminal (full screen, minimized, ‘normal’) has any impact…

Maybe someone with Windows could challenge/confirm my numbers?

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 7:48pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/16 "2020-07-13T19:48:47Z")

</div>

VS Code uses a modified version of Xterm.js.

The size of the terminal window should have minimal impact. Either the terminal supports fast rendering of large texts regardless of window size or it doesn’t.

Does Python version run faster? If so, it is most likely the aforementioned buffering.

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 8:34pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/17 "2020-07-13T20:34:59Z")

</div>

I have found that using this script produces the same output but in hundreds of milliseconds:

```julia
a = []
for i in 1:1000000
	push!(a, i)
end

println("Any[" * join(a, ", ") * "]")

```

Thus, it seems like an issue with printing arrays to me rather than IO in general. I am currently going through the code to try to narrow it down. Should I continue on GitHub or here?

---

<div class="post-metadata">

### Author: ![asmar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmar/32/16358_2.png) [@asmar](https://discourse.julialang.org/u/asmar)
#### Post date: [July 13, 2020, 10:18pm UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/18 "2020-07-13T22:18:10Z")

</div>

I believe that the problem is that when manually joining numbers to a string then the content of the string is printed with single `print` → `write` call in [1] whereas when printing a vector, you end up calling `write` in [1] for each number from [2].

[1] [https://github.com/JuliaLang/julia/blob/5f2bb1d194d220894d130db5be06bec68c726f0c/base/strings/io.jl#L184-L187](https://github.com/JuliaLang/julia/blob/5f2bb1d194d220894d130db5be06bec68c726f0c/base/strings/io.jl#L184-L187)  
[2] [https://github.com/JuliaLang/julia/blob/5f2bb1d194d220894d130db5be06bec68c726f0c/base/show.jl#L973](https://github.com/JuliaLang/julia/blob/5f2bb1d194d220894d130db5be06bec68c726f0c/base/show.jl#L973)

---

<div class="post-metadata">

### Author: ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)
#### Post date: [July 14, 2020, 7:11am UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/19 "2020-07-14T07:11:21Z")

</div>

> [@asmar](#):
>
> Does Python version run faster? If so, it is most likely the aforementioned buffering.

Yes. Python is actually blazing fast 🙂 (about seven seconds (cmd) according to my watch (my Python know-how is near zero)).

The code you posted above is 8 seconds in Julia/cmd. So this is comparable to Python for me.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 14, 2020, 8:35am UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/20 "2020-07-14T08:35:11Z")

</div>

The only time I print tons of data to the REPL is by accident. When I am impatient, I just reach for good old Ctrl-C.

So if this can be fixed trivially, then that would be nice, otherwise I don’t think this is something that needs to be optimized.

[Next page](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987.md?page=2)
