loreson
loreson

Reputation: 132

Improving the performance in fopen/ fclose scenario

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

Answers (2)

Çağrı Uslu
Çağrı Uslu

Reputation: 77

An addition to R..'s answer, you could also catch the interrupt or terminate signal and close the file yourself.

Upvotes: 0

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

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

Related Questions