/// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> private MarshalMemoryBlock CompressBlockMemory(ManualHisDataMemoryBlock data) { MarshalMemoryBlock block = MarshalMemoryBlockPool.Pool.Get(data.Length * 2 + 28 + 5 + 56); var histag = mHisTagService.GetHisTag(data.Id); if (histag == null) { return(null); } int datasize = 0; var targetPosition = 28 + 56; //前56个字节用于统计数据存放 block.WriteInt(56, data.Id); block.WriteDatetime(56 + 4, data.Time); //时间 block.WriteDatetime(56 + 12, data.EndTime); //结束时间 //block.WriteInt(20, 0); //写入数据大小 block.WriteInt(56 + 24, mTagIds.Count);//写入变量的个数 var qulityoffset = data.QualityAddress; var comtype = histag.CompressType;//压缩类型 block.WriteByte(targetPosition + 4, (byte)comtype); var tp = mCompressCach[comtype]; if (tp != null) { tp.QulityOffset = (int)qulityoffset; tp.TagType = histag.TagType; tp.RecordType = histag.Type; tp.StartTime = data.Time; tp.Parameters = histag.Parameters; tp.Precision = histag.Precision; tp.TimeTick = data.TimeUnit; tp.Id = data.Id; var size = tp.Compress(data, 0, block, targetPosition + 5, data.Length, block, 0) + 1; block.WriteInt(targetPosition, (int)size); datasize = (int)(targetPosition + size + 5); block.WriteInt(56 + 20, datasize); //写入数据大小 } return(block); }
public void TimeTest2() { MarshalMemoryBlock memory = new MarshalMemoryBlock((long)(1024 * 1024 * 1024) * 3); DateTime dt = DateTime.Now; long ltmp = (long)(1024 * 1024); memory.WriteByte(ltmp, (byte)20); memory.WriteDatetime(memory.Position, dt); memory.WriteDouble(memory.Position, 20.5); memory.WriteFloat(memory.Position, 20.5f); memory.WriteString(memory.Position, dt.ToString(), Encoding.Unicode); memory.WriteInt(memory.Position, 20); memory.WriteLong(memory.Position, 20); memory.WriteShort(memory.Position, (short)20); memory.WriteUShort(memory.Position, (ushort)20); memory.WriteUInt(memory.Position, 20); memory.WriteULong(memory.Position, 20); MarshalMemoryBlock mm = new MarshalMemoryBlock(1024); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000000; i++) { memory.WriteBytesDirect(500, memory.StartMemory + (int)ltmp, 0, 50); memory.WriteUShortDirect(550, 50); memory.WriteByte(550, 1); } sw.Stop(); Debug.Print("10000000:" + sw.ElapsedMilliseconds); }