Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <code>IConvertible</code>. But the performance difference can be considerable depending on what you need. Compared with a method that use <code>Single</code>, the difference may reach almost 50% more. Doing 100000 iterations, with single takes 109ms and 156ms using Generic.</p> <p>See this code (.Net 2): </p> <pre><code>using System; using System.Text; using NUnit.Framework; namespace ProofOfConcept.GenericInterpolation { /// &lt;summary&gt; /// Proof of concept for a generic Interpolate. /// &lt;/summary&gt; [TestFixture] public class GenericInterpolationTest { /// &lt;summary&gt; /// Interpolate test. /// &lt;/summary&gt; [Test] public void InterpolateTest() { Int16 interpolInt16 = Interpolate&lt;Int16&gt;(2, 4, 5, 6, 7); Int32 interpolInt32 = Interpolate&lt;Int32&gt;(2, 4, 5, 6, 7); Double interpolDouble = Interpolate&lt;Double&gt;(2, 4, 5, 6, 7); Decimal interpolDecimal = Interpolate&lt;Decimal&gt;(2, 4, 5, 6, 7); Assert.AreEqual((Int16)interpolInt32, (Int16)interpolInt16); Assert.AreEqual((Double)interpolDouble, (Double)interpolDecimal); //performance test int qtd = 100000; DateTime beginDt = DateTime.Now; TimeSpan totalTimeTS = TimeSpan.Zero; for (int i = 0; i &lt; qtd; i++) { interpolDouble = Interpolate(2, 4, 5, 6, 7); } totalTimeTS = DateTime.Now.Subtract(beginDt); Console.WriteLine( "Non Generic Single, Total time (ms): " + totalTimeTS.TotalMilliseconds); beginDt = DateTime.Now; for (int i = 0; i &lt; qtd; i++) { interpolDouble = Interpolate&lt;Double&gt;(2, 4, 5, 6, 7); } totalTimeTS = DateTime.Now.Subtract(beginDt); Console.WriteLine( "Generic, Total time (ms): " + totalTimeTS.TotalMilliseconds); } /// &lt;summary&gt; /// Interpolates the specified x1. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="x1"&gt;The x1.&lt;/param&gt; /// &lt;param name="y1"&gt;The y1.&lt;/param&gt; /// &lt;param name="x2"&gt;The x2.&lt;/param&gt; /// &lt;param name="y2"&gt;The y2.&lt;/param&gt; /// &lt;param name="x"&gt;The x.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static T Interpolate&lt;T&gt;(T x1, T y1, T x2, T y2, T x) where T : IConvertible { IConvertible x1C = x1 as IConvertible; IConvertible y1C = y1 as IConvertible; IConvertible x2C = x2 as IConvertible; IConvertible y2C = y2 as IConvertible; IConvertible xC = x as IConvertible; Decimal retDec = y1C.ToDecimal(null) + (xC.ToDecimal(null) - x1C.ToDecimal(null)) * (y2C.ToDecimal(null) - y1C.ToDecimal(null)) / (x2C.ToDecimal(null) - x1C.ToDecimal(null)); return (T)((IConvertible)retDec).ToType(typeof(T), null); } /// &lt;summary&gt; /// Interpolates the specified x1. /// &lt;/summary&gt; /// &lt;param name="x1"&gt;The x1.&lt;/param&gt; /// &lt;param name="y1"&gt;The y1.&lt;/param&gt; /// &lt;param name="x2"&gt;The x2.&lt;/param&gt; /// &lt;param name="y2"&gt;The y2.&lt;/param&gt; /// &lt;param name="x"&gt;The x.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static Single Interpolate(Single x1, Single y1, Single x2, Single y2, Single x) { Single retSing = y1 + (x - x1) * (y2 - y1) / (x2 - x1); return retSing; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload