Profiling one of my C++ programs I just discovered that calling std::ofstream(), when creating a bunch of files, takes much less time than using the system “touch”.
So now I was wondering what OS function the std::ofstream is mapped to, on Linux.
Do you know what std::ofstream() calls to create a file?
Thanks
If you are doing
system("touch filename");this is misleading and slow (and a security risk, and, and …). It doesn’t call the system as such, but spawns a shell, then runs the program (touch in this case) in it.Opening a stream will use some kind of actual system call that can directly access the filesystem. Possibly http://linux.die.net/man/2/open on Linux.
Try running
strace touchin a terminal to find out what system calls it is making. You could probably do the same with a simple c++ program you create just opening a file. Or if you are using an open source implementation (gcc) you can check the source.