public void Test_1000000_array_copy_time_classes_should_be_faster()
        {
            var testSize = 1000000;
            var structArray = new ValueStruct[testSize];
            for (var i = 0; i < testSize; i++)
            {
                structArray[i] = new ValueStruct { AProperty = 1, AField = 1 };
            }
            var start = DateTime.Now;
            var copied = new ValueStruct[testSize];
            structArray.CopyTo(copied, 0);
            var structsEnded = (DateTime.Now - start).TotalMilliseconds;

            var classArray = new ReferenceClass[testSize];
            for (var i = 0; i < testSize; i++)
            {
                classArray[i] = new ReferenceClass { AProperty = 1, AField = 1 };
            }
            start = DateTime.Now;
            var copied2 = new ReferenceClass[testSize];
            classArray.CopyTo(copied2, 0);
            var classesEnded = (DateTime.Now - start).TotalMilliseconds;

            Console.WriteLine($"Classes copied in:{classesEnded}ms");
            Console.WriteLine($"Structs copied in:{structsEnded}ms");
            Assert.IsTrue(classesEnded < structsEnded);
        }
Ejemplo n.º 2
0
 public void Passed_ByRef_instance_properties_can_be_mutated()
 {
     var valueStruct = new ValueStruct();
     Assert.AreEqual(0, valueStruct.AProperty);
     ValueStructMutator.ChangeAProperty(ref valueStruct);
     //Fields are updated as we're updating the instance passed in
     Assert.AreEqual(1, valueStruct.AProperty);
 }
Ejemplo n.º 3
0
 public void Passed_ByVal_instance_properties_cant_be_mutated()
 {
     var valueStruct = new ValueStruct();
     Assert.AreEqual(0, valueStruct.AProperty);
     ValueStructMutator.ChangeAProperty(valueStruct);
     //Because the instance copied is a value type we've copied all its values too
     //This means we end up copying all the values stopping them from being mutally shared
     Assert.AreEqual(0, valueStruct.AProperty);
 }
Ejemplo n.º 4
0
 public void Passed_ByVal_instance_cant_be_mutated()
 {
     var valueStruct = new ValueStruct();
     Assert.AreEqual(0, valueStruct.AField);
     Assert.AreEqual(0, valueStruct.AProperty);
     ValueStructMutator.ChangeTheInstanceOfValueStruct(valueStruct);
     //Changing the instance has no effect as the instance has been copied
     Assert.AreEqual(0, valueStruct.AField);
     Assert.AreEqual(0, valueStruct.AProperty);
 }
Ejemplo n.º 5
0
 public void Passed_ByRef_instance_can_be_mutated()
 {
     var valueStruct = new ValueStruct();
     Assert.AreEqual(0, valueStruct.AField);
     Assert.AreEqual(0, valueStruct.AProperty);
     ValueStructMutator.ChangeTheInstanceOfValueStruct(ref valueStruct);
     //Changing the instance means that the current instance is "updated"
     Assert.AreEqual(1, valueStruct.AField);
     Assert.AreEqual(1, valueStruct.AProperty);
 }
        public void Copied_structs_are_not_mutally_sharing_properties_or_fields()
        {
            var testSize = 1000000;
            var structArray = new ValueStruct[testSize];
            for (var i = 0; i < testSize; i++)
            {
                structArray[i] = new ValueStruct {AProperty = 1, AField = 1};
            }
            var copied = new ValueStruct[testSize];
            structArray.CopyTo(copied, 0);

            for (var i = 0; i < testSize; i++)
            {
                copied[i].AField = 0;
                Assert.AreNotEqual(structArray[i].AField, copied[i].AField);
                copied[i].AProperty = 0;
                Assert.AreNotEqual(structArray[i].AProperty, copied[i].AProperty);
            }
        }
        public void Test_1000000_instance_creation_time_structs_should_be_faster()
        {
            var testSize = 1000000;
            var structArray = new ValueStruct[testSize];
            var start = DateTime.Now;
            for (var i = 0; i < testSize; i++)
            {
                structArray[i] = new ValueStruct {AProperty = 1, AField = 1};
            }
            var structsEnded = (DateTime.Now - start).TotalMilliseconds;

            var classArray = new ReferenceClass[testSize];
            start = DateTime.Now;
            for (var i = 0; i < testSize; i++)
            {
                classArray[i] = new ReferenceClass { AProperty = 1, AField = 1 };
            }
            var classesEnded = (DateTime.Now - start).TotalMilliseconds;

            Console.WriteLine($"Classes creation:{classesEnded}ms");
            Console.WriteLine($"Structs creation:{structsEnded}ms");
            Assert.IsTrue(classesEnded > structsEnded);
        }