Farseer

导航

AX2009里调用.NET DLL的效率问题

经常在AX2009里引用.NET的DLL,因为序列化和反序列化,用.NET的定义的实体方便一些,平时数据量不大,也没觉得有什么问题,今天要把几万条数据从数据库中取出来序列化以后,调用第三方系统的接口,发现很慢,开始以为是从数据库里取数慢,于是优化索引,发现没有任何改善。后来把.NET实体调用部分去掉,很快就完成了。

于是在.NET里用C#写了一段代码做测试

DateTime startTime = DateTime.Now;
            POSHelper.POS.GoodsBarcodeList barcodeList = new POSHelper.POS.GoodsBarcodeList();
            for (int i = 0; i <= 100000; i++)
            {
                POSHelper.POS.GoodsBarcode barcode = new POSHelper.POS.GoodsBarcode();
                barcode.Barcode = "111111";
                barcode.CName = "111111";
                barcode.Code = "111111";
                barcode.EAMU = "111111";
                barcodeList.Add(barcode);
            }
            DateTime endTime = DateTime.Now;
            MessageBox.Show((endTime - startTime).TotalMilliseconds.ToString());

上面这一段代码执行只要20-100毫秒的样子,正常范围。

在X++里写一段等效的代码

POSHelper.POS.GoodsBarcodeList          goodsBarcodeList;
    POSHelper.POS.GoodsBarcode              goodsBarcode;
    System.DateTime                         startTime,endTime;
    System.TimeSpan                         timeSpan;
    int                                     i;
    ;

    new InteropPermission(InteropKind::ClrInterop).assert();

    goodsBarcodeList = new POSHelper.POS.GoodsBarcodeList();
    startTime = System.DateTime::get_Now();
    goodsBarcodeList = new POSHelper.POS.GoodsBarcodeList();

    for(i=1;i<=100000;i++)
    {
        goodsBarcode = new POSHelper.POS.GoodsBarcode();
        goodsBarcode.set_Barcode("1111111");
        goodsBarcode.set_CName("1111111");
        goodsBarcode.set_Code("1111111");
        goodsBarcode.set_EAMU("1111111");
        goodsBarcodeList.Add(goodsBarcode);
    }
    endTime = System.DateTime::get_Now();
    CodeAccessPermission::revertAssert();
    timeSpan = System.DateTime::op_Subtraction(endTime,startTime);
    
    print timeSpan.get_TotalMilliseconds();
    pause;

用了38290毫秒,也就是整整用了38S,搞不懂它在思考什么。
在AX2009里调用.NET类库的效率是让人崩溃的,偶尔数据量小不频繁调用的代码,用用的确蛮方便的,要是数据量大,考虑效率的情况下,还是换个方式吧。。。

posted on 2017-11-15 22:47  佛西亚  阅读(330)  评论(0编辑  收藏  举报