Why is matlab's fopen so slow?

This topic has been permanently closed and transferred to MATLAB Answers.


In our codebase, we want to log strings to a file. I use a very simple function for this:
function log(logstring)
fid = fopen("logging.log","A");
fwrite(fid,logstring);
fclose(fid);
end
Problem is that this is very slow (and I'm already using "A", as recommended for speed).
I also have pyton configured on my pc, which opens up the following alternative way to do the same thing:
function log_python(logstring)
filename = "logging.log";
code = ["with open(filename, 'a',encoding='utf-8',newline='') as f:";
" f.write(data)"];
pyrun(code,data=logstring,filename=filename);
end
This method turns out to be about 10x faster than the matlab version. How is this possible?