Reputation: 19
I am creating an application and I need to create multiple folders and creating folders are inside a go.
Gotta make the most optimized, then I realized that mkdir () is considerably faster than system ('mkdir path');
Does anyone know the reason?
Upvotes: 0
Views: 826
Reputation: 27577
Invoking mkdir
from a shell has loads of overhead (the shell itself, spawning a new process, etc) till it finally calls the same kernel code mkdir()
calls directly.
Upvotes: 0
Reputation: 531878
mkdir()
calls the system call documented by man 2 mkdir
. The function is run within the same process.
system('mkdir path')
forks a new process which runs the mkdir
command, documented by man 1 mkdir
, which despite the same name is a separate command that provides a command-line interface to mkdir
system call.
Upvotes: 2
Reputation: 41321
system ("mkdir path");
calls a program mkdir
, that is spawns a new process with all that it implies.
mkdir()
just calls a system routine.
Upvotes: 3