castclassとisinstの比較


castclassとisinstの比較
ArcObjectsのインタフェース切替ってどんなもんかと思ったが誤差だな。
COMとラッパの差は興味深いかも。

IL 命令
IL_0009: castclass [ESRI.ArcGIS.Geometry]ESRI.ArcGIS.Geometry.IPoint
IL_0009: isinst [ESRI.ArcGIS.Geometry]ESRI.ArcGIS.Geometry.IPoint

int最大値 = ループ数 = 2147483647
_________________
CAST1:158074
AS1:155770
CAST2:555195
AS2:55481

呼び出し側

using (ComReleaser com = new ComReleaser())
{
  var pt = (IPoint)new PointClass();
  com.ManageLifetime(pt);

  var spWatch = new System.Diagnostics.Stopwatch();
  spWatch.Start();

  Test1(pt);

  spWatch.Stop();
  Console.WriteLine("CAST1:{0}", spWatch.ElapsedMilliseconds);
  spWatch.Restart();

  Test2(pt);

  spWatch.Stop();
  Console.WriteLine("AS1:{0}", spWatch.ElapsedMilliseconds);


  Type t = Type.GetTypeFromProgID("esriGeometry.Envelope");
  IEnvelope env = (IEnvelope)Activator.CreateInstance(t);
  com.ManageLifetime(env);
  env.PutCoords(135, 35, 136, 36);
  pt = env.LowerLeft;
  spWatch.Restart();

  Test1(pt);

  spWatch.Stop();
  Console.WriteLine("CAST2:{0}", spWatch.ElapsedMilliseconds);
  spWatch.Restart();

  Test2(pt);

  spWatch.Stop();
  Console.WriteLine("AS2:{0}", spWatch.ElapsedMilliseconds);
}

テスト側

private static void Test1(IPoint pt)
{
  for (int i = 0; i < int.MaxValue; i++)
  {
    var geom = (IGeometry)pt;
    pt = (IPoint)geom;
  }//end loop
}//end method

private static void Test2(IPoint pt)
{
  for (int i = 0; i < int.MaxValue; i++)
  {
    var geom = pt as IGeometry;
    pt = geom as IPoint;
  }//end loop
}
カテゴリー: 開発 タグ: パーマリンク