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); }
/// <summary> /// Tries to get the display unit for the specified dimension. /// </summary> /// <param name="d">The dimension.</param> /// <param name="symbol">The symbol.</param> /// <param name="q">The unit.</param> /// <returns><c>true</c> if the unit was found, <c>false</c> otherwise.</returns> public bool TryGetDisplayUnit(Dimensions d, out string symbol, out DynamicQuantity q) { if (this.displayUnits.TryGetValue(d, out symbol)) { if (this.units.TryGetValue(symbol, out q)) { return true; } } symbol = null; q = default(DynamicQuantity); return false; }
/// <summary> /// Tries to get the unit for the specified symbol. /// </summary> /// <param name="s">The symbol.</param> /// <param name="q">The unit.</param> /// <returns><c>true</c> if the unit was found, <c>false</c> otherwise.</returns> public bool TryGetUnit(string s, out DynamicQuantity q) { return this.units.TryGetValue(s, out q); }
/// <summary> /// Registers the specified unit. /// </summary> /// <param name="s">The symbol.</param> /// <param name="q">The unit.</param> public void Register(string s, DynamicQuantity q) { this.units[s] = q; }
/// <summary> /// Sets the display unit. /// </summary> /// <param name="symbol">The symbol.</param> /// <param name="dynamicQuantity">The dynamic quantity.</param> public void SetDisplayUnit(string symbol, DynamicQuantity dynamicQuantity) { this.displayUnits[dynamicQuantity.Dimensions] = symbol; }
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(); }
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(); }