[英]Moving files to another folder based on a rule Complexity O(n3) optimization
Hello i'm trying to move 25000 of images in 38 folders based on a rule . 您好,我正在尝试根据规则将38个文件夹中的25000张图像移动。 Each folders represents the class the image belongs to .
每个文件夹代表图像所属的类。 I also have 38 .txt files that contain the name of images that belong to that class .
我也有38个.txt文件,其中包含属于该类的图像的名称。 The complexity of this algoritm is O(n3) , more exactly O(25000*38*~3000).
该算法的复杂度为O(n3),更确切地说为O(25000 * 38 *〜3000)。 It took me 12 hours to move just 3000 images.
我花了12个小时才移动3000张图像。 It would take me about ~30 days to move all the images.
移动所有图像大约需要30天。 What can I do to optimize my code ?
我该怎么做才能优化我的代码?
parfor i=1:length(imNames) %imNames is a vector that contains the name of the 25000 images .
for k=1:38 %number of .txt files
nameOfFiles=filenames(k).name;
%filenames stores the names of the 38 .txt files
nameOfFiles=erase(nameOfFiles,'.txt'); % removing .txt extension of files
for j =1:length(data.(nameOfFiles)) % length of a particular .txt file(number of images within a file)
%data is a struct that has 38 fields/classes each field containing the names of images withing a .txt file
if strcmp(char(imNames(i)),data(j).(nameOfFiles))
%Compares value of vector that contains 25000 values with value from file nameOfFiles
copyfile(char(imNames(i)), nameOfFiles); % copies file to a folder nameOfFiles
end
end
end
As nekomantic said parfor actually slowed my program. 正如nekomantic所说,parfor实际上减慢了我的程序。 Also my algorithm could be reduced to O(n2).
我的算法也可以简化为O(n2)。 I didn't actually need the outside for .
我实际上并不需要外面的东西。 I also moved my algorithm in c++ and it moves 1000x faster .
我也在c ++中移动了算法,移动速度提高了1000倍。 Thank you for your help .
谢谢您的帮助 。
for k=1:38
nameOfFiles=filenames(k).name;
nameOfFiles=erase(nameOfFiles,'.txt');
for o=1:length(data.(nameOfFiles))
copyfile(data(o).(nameOfFiles), nameOfFiles);
disp(o)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.