加快代码分析一个文本文件与另一个文本文件的方法

时间:2015-08-29 15:07:48

标签: c#

我创建了一个代码,它基本上分析了120行文本文件的数字与120行文本文件夹的差异。这很慢。我有什么方法可以加快速度?

我将需要遍历这些120行数据点的数百万组。使用数据库要快得多吗?目前需要几分钟才能完成我需要在几秒钟内完成的10k文件

有什么想法吗?

       int numtimes = 0;

        string[] fileEntries = Directory.GetFiles("files", "*", SearchOption.AllDirectories);
        foreach (string fileName in fileEntries)
        {
           //reset rows for next file
          //reset all numbers
           double totalpercent = 1;
           double count = 0;
           string line;
           string line2;
           double price1 = 0;
           double high1 = 0;
           double low1 = 0;
           double price2 = 0;
           double high2 = 0;
           double low2 = 0;
           double pricediff = 0;
           double highdiff = 0;
           double lowdiff = 0;
           double prediction = 0;
           int totalcount = 0;
           int precount = 0;
           int counter;
           System.IO.StreamReader file = new System.IO.StreamReader("1.txt");
           System.IO.StreamReader file2 = new System.IO.StreamReader(fileName);
            while ((line = file.ReadLine()) != null && (line2 = file2.ReadLine()) != null)
           {

                if (totalcount <= 119)
                { 
                //split words
                string[] words = line.Split(';');
                string[] words2 = line2.Split(';');
                //add words to rows
                 //set doubles for calculations
                price1 = Convert.ToDouble(words[0]);
                high1 = Convert.ToDouble(words[1]);
                low1 = Convert.ToDouble(words[2]);
                price2 = Convert.ToDouble(words2[0]);
                high2 = Convert.ToDouble(words2[1]);
                low2 = Convert.ToDouble(words2[2]);
                pricediff = 0;
                highdiff = 0;
                lowdiff = 0;


                    //calc price diff
                    if (price1 == 0 && price2 == 0) { pricediff = 100; }
                    else if (price1 >= 0 && price2 >= 0) { if (price2 < price1) { pricediff = (price2 / price1) * 100; } else { pricediff = (price1 / price2) * 100; } }
                    else if (price1 <= 0 && price2 <= 0) { if (price2 > price1) { pricediff = (price2 / price1) * 100; } else { pricediff = (price1 / price2) * 100; } }
                    else if (price1 <= 0 && price2 >= 0) { if (price2 < price1) { pricediff = (price2 / price1) * 100; } else { pricediff = (price1 / price2) * 100; } }
                    else if (price1 >= 0 && price2 <= 0) { if (price2 > price1) { pricediff = (price2 / price1) * 100; } else { pricediff = (price1 / price2) * 100; } }

                    //calc high diff
                    if (high1 == 0 && high2 == 0) { highdiff = 100; }
                    else if (high1 >= 0 && high2 >= 0) { if (high2 < high1) { highdiff = (high2 / high1) * 100; } else { highdiff = (high1 / high2) * 100; } }
                    else if (high1 <= 0 && high2 <= 0) { if (high2 > high1) { highdiff = (high2 / high1) * 100; } else { highdiff = (high1 / high2) * 100; } }
                    else if (high1 <= 0 && high2 >= 0) { if (high2 < high1) { highdiff = (high2 / high1) * 100; } else { highdiff = (high1 / high2) * 100; } }
                    else if (high1 >= 0 && high2 <= 0) { if (high2 > high1) { highdiff = (high2 / high1) * 100; } else { highdiff = (high1 / high2) * 100; } }
                    //calc low diff
                    if (low1 == 0 && low2 == 0) { lowdiff = 100; }
                    else if (low1 >= 0 && low2 >= 0) { if (low2 < low1) { lowdiff = (low2 / low1) * 100; } else { lowdiff = (low1 / low2) * 100; } }
                    else if (low1 <= 0 && low2 <= 0) { if (low2 > low1) { lowdiff = (low2 / low1) * 100; } else { lowdiff = (low1 / low2) * 100; } }
                    else if (low1 <= 0 && low2 >= 0) { if (low2 < low1) { lowdiff = (low2 / low1) * 100; } else { lowdiff = (low1 / low2) * 100; } }
                    else if (low1 >= 0 && low2 <= 0) { if (low2 > low1) { lowdiff = (low2 / low1) * 100; } else { lowdiff = (low1 / low2) * 100; } }


                    count = count + ((pricediff + highdiff + lowdiff) / 3);
                   }

                if (totalcount > 119)
                {
                    string[] words3 = line2.Split(';');
                    prediction = prediction + Convert.ToDouble(words3[0]);
                    precount ++;
                    textBox10.Text = precount.ToString();
                } 
                totalcount = totalcount + 1;
            }
           file.Close();
           file2.Close();
            textBox2.Text = count.ToString();
            textBox3.Text = (count / 120).ToString();
            numtimes++;
            totalpercent = totalpercent + (count / 120);
            textBox5.Text = numtimes.ToString();
            textBox6.Text = (count / 120).ToString();

            if ((count / 120) > 90)
            {
                string[] row4 = new string[] { (count / 120).ToString(), prediction.ToString() };
                dataGridView4.Rows.Add(row4);
            }

0 个答案:

没有答案