只需为我的朋友写下下面的算法,他正在运营一个呼叫中心,并想格式化他的文件名,并根据月份和日期移动目录。但呼叫中心有超过350万个文件,程序似乎工作了12个小时,仅处理不到20 12
那么有没有任何方法可以优化下面算法,
class Program
{
// How much deep to scan. (of course you can also pass it to the method)
const int HowDeepToScan = 20;
static void Main(string[] args)
{
ProcessDir(@"E:\Hard Disk 2\", 1);
Console.WriteLine("Islem Bitmistir");
Console.ReadLine();
}
public static void ProcessDir(string sourceDir, int recursionLvl)
{
if (recursionLvl <= HowDeepToScan)
{
ChangeDirectories(sourceDir);
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)
ProcessDir(subdir+@"\",recursionLvl + 1);
}
}
public static void ChangeDirectories(string givenPath)
{
DataTable resultSet = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlConnection callCenterConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CallCenter;Data Source=.");
//Directory of mp3s
string sourceDir = givenPath;
//Get the files inside that directory
string[] fileEntries = Directory.GetFiles(sourceDir);
callCenterConnection.Open();
//Iterate through those files
foreach (string fullFileName in fileEntries)
{
//Get the file name without path and extension
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullFileName);
adapter = new SqlDataAdapter("SELECT TOP 1 ID,Time,Tel,AgentID FROM ResultTable WHERE ID=" + fileNameWithoutExtension, callCenterConnection);
adapter.Fill(resultSet);
}
adapter.Dispose();
if (resultSet.Rows.Count != 0)
{
foreach (DataRow dr in resultSet.Rows)
{
DateTime fileDate = Convert.ToDateTime(dr["Time"]);
if (!File.Exists(@"E:\Ses Dosyalari" + @"\" + fileDate.Year + @"\" + fileDate.Month + @"\" + Convert.ToString(dr["Time"]).Replace(":", ".") + " - " + Convert.ToString(dr["Tel"]) + " - " + Convert.ToString(dr["AgentID"]) + ".mp3"))
{
File.Move(sourceDir + Convert.ToString(dr["ID"]) + ".mp3", @"E:\Ses Dosyalari" + @"\" + fileDate.Year + @"\" + fileDate.Month + @"\" + Convert.ToString(dr["Time"]).Replace(":", ".") + " - " + Convert.ToString(dr["Tel"]) + " - " + Convert.ToString(dr["AgentID"]) + ".mp3");
cmd = new SqlCommand("UPDATE ResultTable SET Used = 1 WHERE ID="+Convert.ToString(dr["ID"]), callCenterConnection);
cmd.ExecuteNonQuery();
}
}
}
cmd.Dispose();
callCenterConnection.Close();
resultSet.Clear();
resultSet.Dispose();
}
} 发布于 2012-08-31 15:38:19
瓶颈很可能不是代码的性能特征,而是位置之间的网络。
也就是说,在第二种方法中有很大的优化空间。
首先,总是批量处理数据库中的数据,一次检查一条记录的存在代价很高,一次读取所有记录并将其放入列表中。遍历所述列表以执行移动/状态更新。
其次,您正在使用+连接字符串,请使用String.Format或String.Concat (如果您更喜欢,也可以使用StringBuilder )
由于此操作中最繁重的资源是网络,我建议使用压缩库来包装目录的所有文件(至少),将其传送到远程站点,并让一个进程将其解压缩并发送回完成通知。发送的越少越好。
https://stackoverflow.com/questions/12210612
复制相似问题