Opening and closing file overhead

question
performance

#1

Hello everyone,

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

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

end of function main

or

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

#2

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).


#3

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

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?


#4

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.


#5

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?


#6

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

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

#7

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?


#8

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


#9

Thanks. :slight_smile: