http://blog.csdn.net/lk103852503/article/details/6566652
在写一个对属性表的统计函数时,发现执行速度奇慢无比,百思不得其解,其实算法并不复杂,后来逐句排查终于发现竟是Arcengine的函数读取属性值的问题。
在获取属性表的值时有多种方法:
方法一:
ITable pTable = pLayer.FeatureClass as ITable;
clsFldValue = pTable.GetRow(i).get_Value(3);
方法二:
IFeatureCursor FCursor = pLayer.FeatureClass.Search(new QueryFilterClass(), false);
IFeature feature = FCursor.NextFeature();
if (feature == null) return null;
clsFldValue = feature.get_Value(clsFldIndex);
feature = FCursor.NextFeature();
用Environment.TickCount进行代码执行时间测试,结果发现方法一读取整个表的时间为4984ms,而方法二读取同一个属性给的时间仅为32 ms,法二的执行效率是法一的156倍!!!完整测试代码如下:
IFeatureLayer pLayer = Utilities.GetLayerByName((string)cmbRegLayers.SelectedItem, m_mapControl) as IFeatureLayer;
IFeatureCursor FCursor = pLayer.FeatureClass.Search(new QueryFilterClass(), false);
IFeature feature = FCursor.NextFeature();
int t = Environment.TickCount;
object clsFldValue=null;
for (int i = 0; i < pLayer.FeatureClass.FeatureCount(null); i++)
{
clsFldValue = feature.get_Value(3);
feature = FCursor.NextFeature();
}
t = Environment.TickCount - t;
MessageBox.Show(t.ToString());
ITable pTable = pLayer.FeatureClass as ITable;
t = Environment.TickCount;
for (int i = 0; i < pTable.RowCount(null); i++)
clsFldValue = pTable.GetRow(i).get_Value(3);
t = Environment.TickCount - t;
MessageBox.Show(t.ToString());
至于为什么使用ITable 读取属性值速度如此之慢,不得而知,望有高人指点。
另外,在ESRI中国社区中也有一帖子讨论执行速度的问题,或者能为类似问题做些提示。此贴内容如下:
目标是想将原数据库中的点信息(x,y经纬度坐标,度格式),添加到FeatureClass中,数据库中大概有10000条数据,全部添加到FeatureClass中大概需要半小时以上
DataSet ds = loadExcel("d://aaa.xls");
IFeature feature = featureClass.CreateFeature();
IFields fields = featureClass.Fields;
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
DataRow row = ds.Tables[0].Rows[i];
string xl = Convert.ToString(row[0]);
string x = Convert.ToDouble(row[1]);
string y = Convert.ToDouble(row[2]);
//....其它数据库中字段
//创建点对象
IPoint point = new PointClass();
point.X = x;
point.Y = y;
//设置Fields域
feature.set_Value(fields.FindField("线路"),xl);
feature.set_Value(fields.FindField("经度"),x);
feature.set_Value(fields.FindField("纬度"),y);
//保存点对象
feature.Shape = point;
feature.Store();
}
改进后:
DataSet ds = loadExcel("d://aaa.xls");
IFeatureBuffer featureBuffer;
IFeatureCursor cur = featureClass.Insert(true);
IPoint point;
IFields fields = featureClass.Fields;
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
DataRow row = ds.Tables[0].Rows[i];
string xl = Convert.ToString(row[0]);
string x = Convert.ToDouble(row[1]);
string y = Convert.ToDouble(row[2]);
//....其它数据库中字段
//创建点对象
point = new PointClass();
point.X = x;
point.Y = y;
featureBuffer = featureClass.CreateFeatureBuffer();
//设置Fields域
featureBuffer.set_Value(fields.FindField("线路"),xl);
featureBuffer.set_Value(fields.FindField("经度"),x);
featureBuffer.set_Value(fields.FindField("纬度"),y);
//保存点对象
featureBuffer.Shape = point;
cur.InsertFeature(featureBuffer);
}
可以看出改进后使用了eatureClass.CreateFeatureBuffer方法,使效率大大提高。