我正在尝试提高一些 UI 自动化操作的速度。我偶然发现了(不太好)记录在案的缓存可能性。
据我了解,整个操作(如果你有一个大的 GUI 树)是如此缓慢,因为对于每一个函数调用都必须有一个进程更改(有点像进入内核模式,我想,速度方面?!)。所以..进来缓存。
只需告诉函数缓存一个元素及其子元素,然后以闪电般的速度处理它。(据我了解,您只有一次上下文更改,并一次组装您需要的所有数据。)
好主意,但是.. 它对我来说和未缓存的变化一样慢。我写了一些简单的测试代码,并没有看到改进。
AutomationElement ae; // element whose siblings are to be examined, thre are quite a few siblings
AutomationElement sibling;
#region non-cached
watch.Start();
for (int i = 0; i < 10; ++i)
{
sibling = TreeWalker.RawViewWalker.GetFirstChild(TreeWalker.RawViewWalker.GetParent(ae));
while (sibling != null)
{
sibling = TreeWalker.RawViewWalker.GetNextSibling(sibling);
}
}
watch.Stop();
System.Diagnostics.Debug.WriteLine("Execution time without cache: " + watch.ElapsedMilliseconds + " ms.");
#endregion
#region cached
watch.Reset();
watch.Start();
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Children | TreeScope.Element; // for testing I chose a minimal set
AutomationElement parent;
for (int j = 0; j < 10; ++j)
{
using (cacheRequest.Activate())
{
parent = TreeWalker.RawViewWalker.GetParent(ae, cacheRequest);
}
int cnt = parent.CachedChildren.Count;
for (int i = 0; i < cnt; ++i)
{
sibling = parent.CachedChildren[i];
}
}
watch.Stop();
System.Diagnostics.Debug.WriteLine("Execution time parentcache: " + watch.ElapsedMilliseconds + " ms.");
#endregion
设置是:你得到一个元素并想要检查它的所有(许多)兄弟姐妹。给出了不带和带缓存的两种实现。
输出(调试模式):没有缓存的执行时间:1130 ms。执行时间父缓存:1271 毫秒。
为什么这不起作用?怎么提高?
感谢您的任何想法!!!