测试C# value类型、ref类型、CopyAll工具函数、ZeroFormatter序列化的速度

对于如下结构和类:
<pre>
public struct STPInvestorPositionFieldS
{
///投资者代码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_INVESTORID_LEN)]
public string InvestorID;
///交易所代码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_EXCHANGEID_LEN)]
public string ExchangeID;
///策略代码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_COMBOID_LEN)]
public string ComboID;
///合约代码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_INSTRUMENTID_LEN)]
public string InstrumentID;
///备兑标志
public char CoveredFlag;
///持仓多空方向
public char PosiDirection;
///交易编码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_TRADINGCODE_LEN)]
public string TradingCode;
///交易日
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CommonDef.STP_DATE_LEN)]
public string TradingDay;
///上日持仓
public double YdPosition;
///上次占用的保证金
public double PreMargin;
///今持仓
public double TodayPosition;
///总持仓
public double TotalPosition;
///仓位占用保证金
public double UseMargin;
///开仓量
public double OpenVolume;
///平仓量
public double CloseVolume;
///平昨仓量
public double CloseYdVolume;
///本次结算价
public double SettlementPrice;
///上次结算价
public double PreSettlementPrice;
///手续费
public double Commission;
///冻结的手续费
public double FrozenCommission;
///冻结的保证金
public double FrozenMargin;
///开仓成本
public double OpenCost;
///持仓成本
public double PositionCost;
///平仓盈亏
public double CloseProfit;
///持仓盈亏
public double PositionProfit;
///开仓金额
public double OpenAmount;
///平仓金额
public double CloseAmount;
///平昨仓金额
public double CloseYdAmount;
///多头冻结
public double LongFrozen;
///开仓冻结金额
public double LongFrozenAmount;
///空头冻结
public double ShortFrozen;
///开仓冻结金额
public double ShortFrozenAmount;
};

[ZeroFormattable]
public class STPInvestorPositionField
{
    ///投资者代码
    [Index(0)]
    public virtual string InvestorID { get; set; }
    ///交易所代码
    [Index(1)]
    public virtual string ExchangeID { get; set; }
    ///策略代码
    [Index(2)]
    public virtual string ComboID { get; set; }
    ///合约代码
    [Index(3)]
    public virtual string InstrumentID { get; set; }
    ///备兑标志
    [Index(4)]
    public virtual char CoveredFlag { get; set; }
    ///持仓多空方向
    [Index(5)]
    public virtual char PosiDirection { get; set; }
    ///交易编码
    [Index(6)]
    public virtual string TradingCode { get; set; }
    ///交易日
    [Index(7)]
    public virtual string TradingDay { get; set; }
    ///上日持仓
    [Index(8)]
    public virtual double YdPosition { get; set; }
    ///上次占用的保证金
    [Index(9)]
    public virtual double PreMargin { get; set; }
    ///今持仓
    [Index(10)]
    public virtual double TodayPosition { get; set; }
    ///总持仓
    [Index(11)]
    public virtual double TotalPosition { get; set; }
    ///仓位占用保证金
    [Index(12)]
    public virtual double UseMargin { get; set; }
    ///开仓量
    [Index(13)]
    public virtual double OpenVolume { get; set; }
    ///平仓量
    [Index(14)]
    public virtual double CloseVolume { get; set; }
    ///平昨仓量
    [Index(15)]
    public virtual double CloseYdVolume { get; set; }
    ///本次结算价
    [Index(16)]
    public virtual double SettlementPrice { get; set; }
    ///上次结算价
    [Index(17)]
    public virtual double PreSettlementPrice { get; set; }
    ///手续费
    [Index(18)]
    public virtual double Commission { get; set; }
    ///冻结的手续费
    [Index(19)]
    public virtual double FrozenCommission { get; set; }
    ///冻结的保证金
    [Index(20)]
    public virtual double FrozenMargin { get; set; }
    ///开仓成本
    [Index(21)]
    public virtual double OpenCost { get; set; }
    ///持仓成本
    [Index(22)]
    public virtual double PositionCost { get; set; }
    ///平仓盈亏
    [Index(23)]
    public virtual double CloseProfit { get; set; }
    ///持仓盈亏
    [Index(24)]
    public virtual double PositionProfit { get; set; }
    ///开仓金额
    [Index(25)]
    public virtual double OpenAmount { get; set; }
    ///平仓金额
    [Index(26)]
    public virtual double CloseAmount { get; set; }
    ///平昨仓金额
    [Index(27)]
    public virtual double CloseYdAmount { get; set; }
    ///多头冻结
    [Index(28)]
    public virtual double LongFrozen { get; set; }
    ///开仓冻结金额
    [Index(29)]
    public virtual double LongFrozenAmount { get; set; }
    ///空头冻结
    [Index(30)]
    public virtual double ShortFrozen { get; set; }
    ///开仓冻结金额
    [Index(31)]
    public virtual double ShortFrozenAmount { get; set; }
};
    private STPInvestorPositionField _positionField = new STPInvestorPositionField();
    private STPInvestorPositionField _positionOther = new STPInvestorPositionField();
    private STPInvestorPositionFieldS _positionFieldS = new STPInvestorPositionFieldS();
    private STPInvestorPositionFieldS _positionOtherS = new STPInvestorPositionFieldS();
    public void BenchmarkZFvsCopy()
    {
        HiResTimer timer = new HiResTimer();
        // Get counter value before the operation starts.
        Int64 counterAtStart = timer.Value;
        // 
        for (int i = 0; i < 1000000; ++i)
        {
            _positionField = _positionOther;
        }
        // Get counter value when the operation ends.
        Int64 counterAtEnd = timer.Value;

        // Get time elapsed in tenths of a millisecond.
        Int64 timeElapsedInTicks = counterAtEnd - counterAtStart;
        Int64 timeElapseInTenthsOfMilliseconds =
            (timeElapsedInTicks * 10000) / timer.Frequency;
        Console.WriteLine("Time Spent in operation (tenths of ms) "
                          + timeElapseInTenthsOfMilliseconds);
        //+
        //"\nCounter Value At Start: " + counterAtStart +
        //"\nCounter Value At End : " + counterAtEnd +
        //"\nCounter Frequency : " + timer.Frequency);



        // Get counter value before the operation starts.
        counterAtStart = timer.Value;
        // 
        for (int i = 0; i < 1000000; ++i)
        {
            _positionFieldS = _positionOtherS;
        }
        // Get counter value when the operation ends.
        counterAtEnd = timer.Value;

        // Get time elapsed in tenths of a millisecond.
        timeElapsedInTicks = counterAtEnd - counterAtStart;
        timeElapseInTenthsOfMilliseconds =
            (timeElapsedInTicks * 10000) / timer.Frequency;
        Console.WriteLine("Time Spent in operation (tenths of ms) "
                          + timeElapseInTenthsOfMilliseconds);


        // Get counter value before the operation starts.
        counterAtStart = timer.Value;
        // 
        for (int i = 0; i < 1000000; ++i)
        {
            //_positionFieldS = _positionOtherS;
            BenchmarkUtil.BinaryUtil.CopyAllS2C(_positionOtherS, _positionField);
        }
        // Get counter value when the operation ends.
        counterAtEnd = timer.Value;

        // Get time elapsed in tenths of a millisecond.
        timeElapsedInTicks = counterAtEnd - counterAtStart;
        timeElapseInTenthsOfMilliseconds =
            (timeElapsedInTicks * 10000) / timer.Frequency;
        Console.WriteLine("Time Spent in operation (tenths of ms) "
                          + timeElapseInTenthsOfMilliseconds);


        // Get counter value before the operation starts.
        counterAtStart = timer.Value;
        // 
        for (int i = 0; i < 1000000; ++i)
        {
            var bytes = ZeroFormatterSerializer.Serialize(_positionOther);
            ZeroFormatterSerializer.Deserialize<STPInvestorPositionField>(bytes);
            ;
        }
        // Get counter value when the operation ends.
        counterAtEnd = timer.Value;

        // Get time elapsed in tenths of a millisecond.
        timeElapsedInTicks = counterAtEnd - counterAtStart;
        timeElapseInTenthsOfMilliseconds =
            (timeElapsedInTicks * 10000) / timer.Frequency;
        Console.WriteLine("Time Spent in operation (tenths of ms) "
                          + timeElapseInTenthsOfMilliseconds);

    }

</pre>

分别执行1000000次,几种赋值方式的耗时分别是:
Debug模式 Release模式
Class赋值: 25ms, 13ms
struct赋值: 750ms, 750ms
copyall: 211057ms 209083ms
zeroformatter 序列化+反序列化 22231ms 11971ms

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 三绝·官商 现金流,滚雪球, 关系网,内外愁!
    補缺楼丨胡德棒阅读 278评论 2 2
  • 精进 经历了共享单车,共享雨伞,今天社会上有了共享睡眠空间,随时随地可以用微信扫一扫,你就可以睡觉。他解决了上班族...
    若兰ZHOU阅读 101评论 0 1
  • 郭相麟 一位母亲在一九九五年四月七日生下了可爱,帅气的儿子,儿子头发自然微卷,大眼睛闪烁着天真,灵性,一家人生...
    郭相麟阅读 201评论 0 0