Notifications
Clear all

[Closed] doesFileExist = Slow

 lo1

And the results, using Josef’s script:

MXS:	111
NET:	166
SHL:	171
Caching:58

Of course, this testing scenario is the absolute best case scenario for the algorithm I outlined, as we are checking every file in the folder.

A worst case scenario would be checking only 1 file out of a very large folder.

Here is my implementation:

global DirectoriesCache = #()
global FilesCache = #()

function DoesFileExistCached filename =
(
	local filePath = getfileNamePath filename
	local ind = findItem DirectoriesCache filePath
	if (ind == 0) do
	(
		append DirectoriesCache filePath
		append FilesCache (getFiles (filePath + "*.*"))
		ind = DirectoriesCache.count
	)
	return findItem FilesCache[ind] filename > 0
)

I opted to use two MXS arrays in this case instead of a .NET dictionary to avoid the penalties of the string conversions.

 lo1

Here’s an even faster version, moving the caching to an assembly:

	source += "	private Dictionary<string, string[]> FileCache;

"
	
	source += "	public FileIO()
"
	source += "	{
"
	source += "		FileCache = new Dictionary<string, string[]>();
"
	source += "	}
"
	
	source += "	public bool DoesFileExist(string filename)
"
	source += "	{
"
	source += "		string dir = Path.GetDirectoryName(filename);
"
	source += "		if (!FileCache.ContainsKey(dir))
"
	source += "		{
"
	source += "			FileCache[dir] = Directory.GetFiles(dir);
"
	source += "		}
"
	source += "		return Array.IndexOf(FileCache[dir], filename) >= 0;
"
	source += "	}
"

If you’re using a new version of 3dsmax (.NET 3.5 and up), you can try using a HashSet instead of an array for the files, that should prove even faster as lookup times are o(1) instead of o(n).

hm – how would this caching approach deal with files getting deleted, problably from an external application ? As i understand, it would still report files as existing, once they are cached, as they would’nt be removed from the cache …

1 Reply
 lo1
(@lo1)
Joined: 11 months ago

Posts: 0

Naturally, information would only be accurate at the time of caching. Whether or not this is important depends on the context of usage.

Page 2 / 2