I have to write a program in Matlab, the performance is quite important since it handles a lot of files.
I have to find the last file extension of a filename and split it there.
e.g. file.name.tar.gz should be split into file.name.tar and gz
I identified 3 methods to do this but I don’t know which is the fastest.
1.
javaFilename = java.lang.String(fileName);
lastDot = javaFilename.lastIndexOf('.');
name = char(javaFilename.substring(0, lastDot-1));
ext = char(javaFilename.substring(lastDot+1));
2.
dots = findstr(fileName, '.');
lastDot = dots(length(dots));
name = fileName(1:lastDot-1);
ext = fileName(lastDot+1:end);
3.
[name tempExt] = strtok(fileName, '.');
while tempExt
[temp2 tempExt] = strtok(fileName, '.');
if tempExt
name = strcat(name, '.', temp2);
end
end
ext = temp2(2:end);
I think the third one is the worst. But how about the other methods?
Can you come up with anything that is faster than the methods described by me?
You can test these by doing something like:
I benchmarked yours and found the second one was much faster.
My solution would be
The times I got were:
Java Solution: 23 seconds
findstr Solution: 0.4 seconds
strtok Solution: (didn’t terminate; maybe I copied it wrong)
reverse for-loop Solution: 0.01 seconds
The advantage of my solution over findstr and strtok is that:
a) it only looks for one dot, rather than all of them
b) it starts from the end of the string (presumably most file extensions will be 2-4 letters long).