对于如下结构和类:
<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