【发布时间】:2017-07-07 07:36:16
【问题描述】:
我正在创建一个指纹验证系统,在该系统中我必须使用数据库中的记录来匹配指纹。我已经使用了foreach 循环,但仅 350 条记录就需要将近 40 秒。我想加快速度。我希望我的 foreach 循环转换为 for 循环,但我在初始化 for 循环时遇到了一些困难。这是我的代码。
foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic)
{
this.Invoke(new Function(delegate()
{
textBox1.Text += "\n" + tt.Key.ToString();
}));
temp = new DPFP.Template(tt.Value);
Verificator = new DPFP.Verification.Verification();
featuresForVerification = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Verification);
if (featuresForVerification != null)
{
DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();
Verificator.Verify(featuresForVerification, temp, ref result);
#region UI Envoke
this.Invoke(new Function(delegate()
{
if (result.Verified)
{
MessageBox.Show("FAR " + result.FARAchieved + "\n" + tt.Key.ToString());
}
else
{
//MessageBox.Show("No Match");
}
}));
#endregion
}
this.Invoke(new Function(delegate()
{
progressBar1.Value += 1;
}));
Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
我对第一行 foreach (KeyValuePair<decimal, MemoryStream> tt in profileDic) 感到困惑。有人可以告诉我如何使用 for 循环遍历 Dictionary 对象profileDic 中的每个项目。我不确定在使用for 循环时如何获得KeyValuePair<decimal, MemoryStream> tt in profileDic。
【问题讨论】:
-
是什么让您认为
for循环会提高速度?你有没有运行分析器来找出哪个部分实际上很慢? -
除了@JamesThorpe,您可能还想检查您的数据库,如果您的查询没有跟上速度,它不会有很大改进。
-
Verificator = new DPFP.Verification.Verification();;这条线有效吗?如果是这样,Verificator是一个对象,而不是一个类,所以应该称为verificator。您是否需要在每次迭代时重新初始化该对象,还是允许您重用同一个对象,多次调用Verify而不会影响结果? -
“你能告诉我哪个代码需要更多时间吗?” 不,但是一个好的分析器可以。以 Ants 或 dotTrace 为例。
-
性能分析初学者指南(在 Visual Studio 中)msdn.microsoft.com/en-us/library/ms182372.aspx
标签: c# performance for-loop foreach performance-testing