Fastest way to implement mktree in FTP
Generally, the fastest algorithm for recursive directory creation (similar to UNIX mkdir -p) using FTP?
I looked at one approach:
- MKDIR node
- if error and nodes stayed at 1 with next node
- end
But this can have poor performance if part of the directory most likely exists. For example, with some depreciation, the "/ a / b / c / d" part of the path "/ a / b / c / d / e / f / g" exists% 99 of the time.
Considering that sending a command and receiving a response takes most of the time, the fastest way to create a directory path is to use as few commands as possible.
Since there is no way to create or cd a directory to check for its existence, simply using mkdir a; mkdir a / b; ..., mkdir a / b / c / d / e / f would be the fastest way (not cd into subdirectories to create the next one as it will take the process longer).
If you create multiple directories this way, you can of course keep track of which top-level directories you have already created. Also, depending on the length of your paths and the likelihood that the top directories already exist, you might want to try starting with eg. mkdir a / b / c (for a / b / c / d / e / f) and then back if it failed. However, if it is more likely that the directories do not exist, it will eventually be slower.
If the existing directory hierarchy is equally likely to terminate at any given depth, then a quick search for the starting position would be the fastest way. But as dseifert points out , if in most cases directories already exist up to level k, then it will be faster to start a binary search at level k rather than at level n / 2.
By the way, you will need to create many very deep directories for this kind of optimization to be worth your time. Are you sure you are not optimizing prematurely?