一步一步测,看看是Deserialize时慢,还是转MP时慢。

解决方案 »

  1.   

    Deserialize时慢,我还测试了一下,把我的MP对象中的DataTable里的计算字段(expression字段)去掉了,就快了许多,只用了90ms,表里大概一百多条数据,原来要60s,看来是这些表达式字段使反序列化慢了近千倍.
      

  2.   

    iwteih()说的对,不是Deserialize时慢,是处理mp对象中的DataTable慢,看来Expression不能乱用,效率不高,想了一个变通的方法,把DataTable转储在一个对象数组中,而不是直接序列化DataTable,然后保存各个字段的Expression字符串,在反序列化之后重建DataTable,添加完数据后,再对各个计算字段设置Expression.现在发现计算字段还是比较不实用的,对于简单处理还行,如果批量处理数据时,计算起来可就慢了,没发现停止计算的属性,只好先清掉Expression,处理完再加上去,呵呵.代码如下,供参考.
    public static void BinaryDeserialize(string sourceFile)
            {
                BinaryFormatter bf = new BinaryFormatter();
                IFormatter formatter = (IFormatter)bf;
                FormatterTypeStyle fts = bf.TypeFormat;
                bf.TypeFormat = fts | FormatterTypeStyle.XsdString;            StreamReader sr = new StreamReader(sourceFile);            MP mp = null;
                try
                {
                    mp = (MP)bf.Deserialize(sr.BaseStream);
                }
                catch
                {
                    return null;
                }
                finally
                {
                    sr.Close();
                }
                            for (int i = 0; i < mp.colNames.Count; i++)
                {
                    DataColumn col = new DataColumn(mp.colNames[i].ToString(),
                        Type.GetType(mp.colTypes[i].ToString()));
                    mpCase.CaseData.Columns.Add(col);
                }            // Add rows
                for (int i = 0; i < mp.dataRows.Length; i++)
                {
                    DataRow row = mpCase.CaseData.NewRow();
                    row.ItemArray = (object[])mp.dataRows[i];
                    mpCase.CaseData.Rows.Add(row);
                }            for (int i = 0; i < mpCase.CaseData.Columns.Count; i++)
                {
                    if (mp.colExpressions[i].ToString()!=string.Empty)
                        mpCase.CaseData.Columns[i].Expression =mp.colExpressions[i].ToString();
                }            ......
            }
    [Serializable]
        public class MP
        {
            public MP(int rowCount)
            {
                colNames = new ArrayList();
                colTypes = new ArrayList();
                colExpressions = new ArrayList();
                dataRows = new Object[rowCount];
                factors = new ArrayList();
                layers = new ArrayList();
                layerfactors = new ArrayList();
                charts = new ArrayList();
            }        public ArrayList colNames;
            public ArrayList colTypes;
            public ArrayList colExpressions;
            public Object[] dataRows;        public String caseName;
            public String client;
            public String product;
            public String caseDesc;
            public DateTime caseDate;
            public Boolean layerMode;        public ArrayList factors;
            public ArrayList layers;
            public ArrayList layerfactors;
            public ArrayList charts;    }
    谢谢大家的帮助.如有更好解决方案,请提供.msn:[email protected]