public void Parse_Speed() { var up = new DynamicUnitProvider(CultureInfo.InvariantCulture, typeof(SI)); var speed = DynamicQuantity.Parse("3.6 km/h", up); Assert.AreEqual(1, speed.Value); Assert.AreEqual(1, speed.Dimensions.Length); Assert.AreEqual(-1, speed.Dimensions.Time); }
static IEnumerable <long> TestQuantityTypesDynamic(int N) { Console.WriteLine("=== QuantityTypes (dynamic) ==="); var length = Marshal.SizeOf(typeof(DynamicQuantity)); Console.WriteLine("Length = {0} bytes", length); yield return(length); var a1 = new DynamicQuantity[N]; var a2 = new DynamicQuantity[N]; using (var timer = new Timer("Assign")) { for (int i = 0; i < N; i++) { a1[i] = SI.Metre; } } using (var timer = new Timer("Assign (multiplication)")) { for (int i = 0; i < N; i++) { a1[i] = i * SI.Metre; } yield return(timer.Stop()); } using (var timer = new Timer("Assign (ctor)")) { for (int i = 0; i < N; i++) { a1[i] = new DynamicQuantity(i, new Dimensions(0, 1, 0)); } yield return(timer.Stop()); } using (var timer = new Timer("Add (operator)")) { for (int i = 0; i < N; i++) { a1[i] = a1[i] + a1[0]; } yield return(timer.Stop()); } using (var timer = new Timer("Add (property)")) { for (int i = 0; i < N; i++) { a1[i] = new DynamicQuantity(a1[i].Value + a1[0].Value, new Dimensions(0, 1, 0)); } yield return(timer.Stop()); } var s = "10"; using (var timer = new Timer("Parse")) { // for (int i = 0; i < N; i++) a1[i] = DynamicQuantity.Parse(s); yield return(timer.Stop()); } s = "10 m"; using (var timer = new Timer("Parse with unit")) { for (int i = 0; i < N; i++) { a1[i] = DynamicQuantity.Parse(s); } yield return(timer.Stop()); } using (var timer = new Timer("Multiply")) { for (int i = 0; i < N; i++) { a2[i] = a1[i] * a1[i]; } yield return(timer.Stop()); } using (var timer = new Timer("Multiply (property, ctor)")) { for (int i = 0; i < N; i++) { a2[i] = new DynamicQuantity(a1[i].Value * a1[i].Value, new Dimensions(0, 2, 0)); } yield return(timer.Stop()); } double sum1; using (var timer = new Timer("Sum (operator)")) { var sum = new DynamicQuantity(); for (int i = 0; i < N; i++) { sum += a2[i]; } sum1 = sum.ConvertTo(SI.SquareMetre); yield return(timer.Stop()); } double sum3 = 0; using (var timer = new Timer("Sum (property)")) { for (int i = 0; i < N; i++) { sum3 += a2[i].Value; } yield return(timer.Stop()); } Console.WriteLine(); }