Reputation: 132
The following situation: I'm editing a single method in a huge Program, which gets called very often, but I don't know how often exactly(different for each run).
Now I have to add logging to a txt file, and opening/closing the file on each call is a huge bottleneck for the whole application.
Just leaving the file open isn't realy an option, because I don't know if its the last call to this function.
Is there a way to speed this up without modifying the calling site?
I'd rather avoid changing the method signature, because that would cause problems when we combine the work with my coworkers( no working version control here)
Upvotes: 1
Views: 1051
Reputation: 77
An addition to R..'s answer, you could also catch the interrupt or terminate signal and close the file yourself.
Upvotes: 0
Reputation: 215287
Leave the file open. All files are closed (which includes flushing any pending buffers) when your program calls exit
or returns from main
. There is no reason to care whether a call to the log function is the "last call".
Upvotes: 1