How can i make the FOR loop to work faster on large files ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I extracted to the hard disk 4800 images(frames) when im running the program its calling the function DoThresholdCheck()
Inside there im running on a LIST<> to check if the files i extracted are on the hard disk according to the threshold...But this loop over the LIST<> taking very long time.

This is the loop part:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < myNumbers.Count; i++)
{
<span style="color:Blue; if (myNumbers >= max_min_threshold)
{



FileName = i.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp";
<span style="color:Blue; if (File.Exists(subDirectoryName + "\" + FileName))
{
counter = counter + 1;
button1.Enabled = <span style="color:Blue; false;
myTrackPanelss1.trackBar1.Enabled = <span style="color:Blue; true;
trackbarTrueFalse = <span style="color:Blue; true;
label9.Visible = <span style="color:Blue; true;
_fi = <span style="color:Blue; new DirectoryInfo(subDirectoryName).GetFiles(<span style="color:#A31515; "*.bmp");
label15.Text = _fi.Length.ToString();



}
<span style="color:Blue; else
{
trackbarTrueFalse = <span style="color:Blue; false;
label8.Visible = <span style="color:Blue; false;
label9.Visible = <span style="color:Blue; false;
button1.Enabled = <span style="color:Blue; true;
myTrackPanelss1.trackBar1.Enabled = <span style="color:Blue; false;
myTrackPanelss1.trackBar1.Value = 0;
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
label5.Location = <span style="color:Blue; new Point(441, 23);
label5.Text = <span style="color:#A31515; "Demo Image";
label5.ForeColor = Color.Red;
label5.Visible = <span style="color:Blue; true;
<span style="color:Green; //secondPass = true;
<span style="color:Blue; break;
}


}
}
[/code]


And this is the complete DoThresholdCheck() function:

<pre>public void DoThresholdCheck()
{
string FileName = null;
bool trackbarTrueFalse = false ;
List<int> fts;
double thershold = 0;
double max_min_threshold = 0;
//myTrackPanelss1.trackBar1.Value = 0;
counter = 0;
myNumbers = new List<double>();
_videoFile = Options_DB.get_loadedVideo();
if (_videoFile == null)
{
return;
}
else
{
checkBox1.Enabled = true;
button2.Enabled = true;
DirectoryInfo inf = new DirectoryInfo(_videoFile);
directoryName = inf.Name;
label6.Visible = true;
label13.Visible = true;
label13.Text = directoryName;
averagesListTextFileDirectory = directoryName;
averagesListTextFile = directoryName;
DirectoryInfo outputINfo = new DirectoryInfo(_outputDir);
string outINf = outputINfo.Root.Name;
if (_outputDir == outINf)
{
subDirectoryName = _outputDir + averagesListTextFileDirectory;
}
else
{
subDirectoryName = _outputDir + "\" + averagesListTextFileDirectory;
}
}

if (!Directory.Exists(subDirectoryName))
{

Directory.CreateDirectory(subDirectoryName); // to check when program is virgin why when set to d:frames its giving me access denied message !!! and this line is exception
// since subdirectoryname is null since access denied when selected d:frames as output directory.
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
}
if (Directory.Exists(subDirectoryName))
{


_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
if (!File.Exists(subDirectoryName + "\" + averagesListTextFile + ".txt"))
{
label8.Visible = false;
label9.Visible = false;
trackbarTrueFalse = false;
button1.Enabled = true;
myTrackPanelss1.trackBar1.Enabled = false;
myTrackPanelss1.trackBar1.Value = 0;
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
label5.Location = new Point(441, 23);
label5.Text = "Demo Image";
label5.ForeColor = Color.Red;
label5.Visible = true;
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
}
else
{

numericUpDown1.Value = Options_DB.Get_numericUpDownValue();
thershold = (double)numericUpDown1.Value;
reader = new StreamReader(subDirectoryName + "\" + averagesListTextFile + ".txt");
string line;
Regex numberPattern = new Regex(@"^(d|.)+$");
while ((line = reader.ReadLine()) != null)
{
if (numberPattern.IsMatch(line))
{
double myLine = Convert.ToDouble(line);
myNumbers.Add(myLine);

}
}

reader.Close();
max = myNumbers.Max();
min = myNumbers.Min();
max_min_threshold = (thershold / 100) * (max - min) + min;
_fi = new DirectoryInfo (subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
for (int i = 0; i < myNumbers.Count; i++)
{
if (myNumbers >= max_min_threshold)
{



FileName = i.ToString("D6") + ".bmp";
if (File.Exists(subDirectoryName + "\" + FileName))
{
counter = counter + 1;
button1.Enabled = false;
myTrackPanelss1.trackBar1.Enabled = true;
trackbarTrueFalse = true;
label9.Visible = true;
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();



}
else
{
trackbarTrueFalse = false;
label8.Visible = false;
label9.Visible = false;
button1.Enabled = true;
myTrackPanelss1.trackBar1.Enabled = false;
myTrackPanelss1.trackBar1.Value = 0;
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
label5.Location = new Point(441, 23);
label5.Text = "Demo Image";
label5.ForeColor = Color.Red;
label5.Visible = true;
//secondPass = true;
break;
}


}
}

if (trackbarTrueFalse == false) // not working to keep checking !!!!!
{

pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
label5.Location = new Point(441, 23);
label5.Text = "Demo Image";
label5.ForeColor = Color.Red;
label5.Visible = true;
myTrackPanelss1.trackBar1.Enabled = false;
myTrackPanelss1.trackBar1.Value = 0;
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
}
else
{
checkBox2.Checked = true;
myTrackPanelss1.trackBar1.Maximum = counter;
myTrackPanelss1.trackBar1.Value = 0;
setpicture(0);
myTrackPanelss1.trackBar1.Scroll += new EventHandler(trackBar1_Scroll);
}

if (_fi.Length >= 0)
{
label15.Text = _fi.Length.ToString();
label15.Visible = true;
}
}
}
else
{
button1.Enabled = true;
}






}[/code]
<br/>
I must call it in the constructor somewhere since i want to show in the pictureBox1 only frames(images) from the hard disk in some conditions.
If its only 10-20-30 images its fast but now it suppoade to scan 4800 images and in this case to show them all and its taking a very long time.

Any way to make it faster ?

Thanks.

View the full article
 
Back
Top