# Opening and closing file overhead

**URL:** <https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635>\
**Category:** New to Julia\
**Tags:** question, performance\
**Created:** [May 1, 2018, 12:30pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635 "2018-05-01T12:30:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 1, 2018, 12:30pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/1 "2018-05-01T12:30:59Z")

</div>

Hello everyone,

I`m somehow new to programming. I have a question. Which algorithm has a better speed?  
which one do you recommend?

```julia
function main()
   opening a file
   for loop 
       <doing some stuf>
        writing something to file
   end loop
   closing file

end of function main

```

or

```julia
function printer(What should be written)
     opening file
     writing in file
     closing file
end

function main()
   for loop
       <doing some stuf>
        printer(What should be written)
   end loop
end of function main

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 1, 2018, 12:42pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/2 "2018-05-01T12:42:09Z")

</div>

Optimization is mostly about to as little as possible so you should avoid keep opening and closing files unless the resource consumed by keeping one file open all the time during `<doing some stuff>` is somehow unacceptable (usually not the case).

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 1, 2018, 12:52pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/3 "2018-05-01T12:52:15Z")

</div>

Thanks.  
I have found this answer convincing.  
but I have a question. when I keep the file open during the loop. I want to see what is writing inside the file so I use the

```julia
tail -f FILENAME

```

command in the Linux operation system. I see nothing. Is that mean that `write()`function is doing nothing unless it reaches the `closefile` function or I`m missing something?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 1, 2018, 12:57pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/4 "2018-05-01T12:57:14Z")

</div>

The output is likely being buffered (that’s true for most I/O libraries in different languages).  
You’d need to flush the output before it can be seen by other processes.

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 1, 2018, 1:01pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/5 "2018-05-01T13:01:20Z")

</div>

yes, I am aware of that. I am using `flush(STDOUT);` right now but the problem still exist. maybe I`m flushing the wrong buffer?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 1, 2018, 1:03pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/6 "2018-05-01T13:03:56Z")

</div>

Yes, you need to flush the buffer you’re writing to like so:

```julia
open("foo.txt", "w") do io
  write(io, "hi")
  flush(io)
end

```

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 1, 2018, 1:06pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/7 "2018-05-01T13:06:06Z")

</div>

Thanks. I will do that.  
how can I say that write something at the end of the opened file and do not overwrite the file?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 1, 2018, 1:20pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/8 "2018-05-01T13:20:09Z")

</div>

> [@alirezamecheng](#):
>
> how can I say that write something at the end of the opened file and do not overwrite the file?

`open("foo.txt", "a") do io`

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 1, 2018, 1:25pm UTC](https://discourse.julialang.org/t/opening-and-closing-file-overhead/10635/9 "2018-05-01T13:25:32Z")

</div>

Thanks. 🙂
