# 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:** 1
**Showing post:** 22

<div class="post-metadata">

### Author: ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)
#### Post date: [October 29, 2020, 8:56am UTC](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987/22 "2020-10-29T08:56:21Z")

</div>

> [@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 different use of printing to stdout/stderr is to pipe output through `less` (or similar pager) to be able to inspect the output interactively without having to do a full (long) run. E.g. grep for certain strings in the output to check a condition, then letting the program continue until the next matching line, etc. I find this very useful during development.

In this case you will also hit upon the slower output of Julia compared to Python. A simple (rough, due to the user keyboard interaction) test of printing one integer per line for 1 - 10,000,000 and piping through `less` then using `>` and `q` to jump to the end of the output to let the script finish:

```julia
paulm@cmstorm 09:38:/data/examples/julia$ cat print_integers.py 
for i in range(1, 10000001):
    print(i)

paulm@cmstorm 09:38:/data/examples/julia$ cat print_integers.jl 
for i in 1 : 10000000
    println(i)
end

paulm@cmstorm 09:38:/data/examples/julia$ time python print_integers.py | less
<press ">" followed by "q" and wait for script to finish>

real	0m3.474s
user	0m3.723s
sys	0m0.080s

paulm@cmstorm 09:38:/data/examples/julia$ time julia -O3 print_integers.jl | less
<press ">" followed by "q" and wait for script to finish>

real	0m53.254s
user	0m39.575s
sys	0m47.758s

```

This is actually quite an eye-opener, to see the insane number of syscalls Julia does compared to Python:

```julia
paulm@cmstorm 09:43:/data/examples/julia$ strace -c -o out.python python print_integers.py | less
<press ">" followed by "q" and wait for script to finish>

paulm@cmstorm 09:43:/data/examples/julia$ strace -c -o out.julia julia -O3 print_integers.jl | less
<press ">" followed by "q" and wait for script to finish>

paulm@cmstorm 09:54:/data/examples/julia$ grep write out.python 
 18.68 0.001243 0 9626 write
paulm@cmstorm 09:54:/data/examples/julia$ grep wait out.python 
paulm@cmstorm 09:54:/data/examples/julia$ grep write out.julia 
 41.49 49.752976 2 20000003 write
  0.00 0.000010 0 13 pwrite64
paulm@cmstorm 09:55:/data/examples/julia$ grep wait out.julia 
 58.49 70.127743 1 40000001 epoll_pwait

```

---

_[View the full topic](https://discourse.julialang.org/t/why-is-printing-to-a-terminal-slow/42987)._
