static void Main(string[] args) { RoomTypeEntity roomEntity = new RoomTypeEntity(); int count = 1000000; bool isBuyoutRoom = false; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < count; i++) { RoomTypeEntity tempRoomTypeEntity = roomEntity.CloneRoomTypeEntity(); if (tempRoomTypeEntity == null) { tempRoomTypeEntity = new RoomTypeEntity(); } } sw.Stop(); long sa1 = sw.ElapsedMilliseconds; Console.WriteLine("CloneRoomTypeEntity:" + sw.ElapsedMilliseconds); sw.Restart(); for (int i = 0; i < count; i++) { RoomTypeEntity tempRoomTypeEntity = new RoomTypeEntity(); tempRoomTypeEntity.IsBuyoutRoom = isBuyoutRoom; tempRoomTypeEntity.RoomStatus = roomEntity.RoomStatus; } sw.Stop(); Console.WriteLine("NewRoomTypeEntity:" + sw.ElapsedMilliseconds); }
public RoomTypeEntity CloneRoomTypeEntity() { RoomTypeEntity result = null; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); stream.Position = 0; result = formatter.Deserialize(stream) as RoomTypeEntity; } return(result); }