一部のUIオートメーション操作の速度を上げようとしています。私は(あまりよくない)文書化されたキャッシュの可能性に出くわしました。
私が理解したことから、操作全体(大きなGUIツリーがある場合)は非常に遅いです。なぜなら、関数呼び出しごとにプロセスを変更する必要があるからです(カーネルモードに移行するようなものだと思います。速度的に?!)。だから..キャッシングがやってくる。
要素とその子をキャッシュするように関数に指示するだけで、非常に高速に処理できます。(私が理解していることから、コンテキスト変更は1つだけであり、必要なすべてのデータを一度にアセンブルします。)
良い考えですが、キャッシュされていないバリエーションと同じくらい遅いです。簡単なテストコードをいくつか書きましたが、改善は見られませんでした。
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ミリ秒。実行時間parentcache:1271ミリ秒。
なぜこれが機能しないのですか?改善方法?
アイデアをありがとう!!!