Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Reflector shows test2() turned into 4 classes while test1() is turned into two classes. This only happens in debug mode. Reflector shows identical code (one class for each) in release mode. Unfortunately Reflector is crashing when I try to view the source in C# and the IL is really long.</p> <pre><code>let test1() = let gL = G_of 1L [1L..1000000L] |&gt; List.map (fun n -&gt; factorize gL n) let test2() = [1L..1000000L] |&gt; List.map (fun n -&gt; factorize (G_of 1L) n) </code></pre> <p>A quick benchmark.</p> <pre><code>let sw = Stopwatch.StartNew() test1() |&gt; ignore sw.Stop() Console.WriteLine("test1 {0}ms", sw.ElapsedMilliseconds) let sw2 = Stopwatch.StartNew() test2() |&gt; ignore sw2.Stop() Console.WriteLine("test2 {0}ms", sw2.ElapsedMilliseconds) </code></pre> <p>Benchmarks ran on I7 950 @3368Mhz, windows 7 64bit, VS2010 F#2.0</p> <p>x86 Debug<br> <code>test1 8216ms</code><br> <code>test2 8237ms</code></p> <p>x86 Release<br> <code>test1 6654ms</code><br> <code>test2 6680ms</code></p> <p>x64 Debug<br> <code>test1 10304ms</code><br> <code>test2 10348ms</code></p> <p>x64 Release<br> <code>test1 8858ms</code><br> <code>test2 8977ms</code></p> <p>Here is the complete code.</p> <pre><code>open System open System.Diagnostics let inline zero_of (target:'a) : 'a = LanguagePrimitives.GenericZero&lt;'a&gt; let inline one_of (target:'a) : 'a = LanguagePrimitives.GenericOne&lt;'a&gt; let inline two_of (target:'a) : 'a = one_of(target) + one_of(target) let inline three_of (target:'a) : 'a = two_of(target) + one_of(target) let inline negone_of (target:'a) : 'a = zero_of(target) - one_of(target) let inline any_of (target:'a) (x:int) : 'a = let one:'a = one_of target let zero:'a = zero_of target let xu = if x &gt; 0 then 1 else -1 let gu:'a = if x &gt; 0 then one else zero-one let rec get i g = if i = x then g else get (i+xu) (g+gu) get 0 zero type G&lt;'a&gt; = { negone:'a zero:'a one:'a two:'a three:'a any: int -&gt; 'a } let inline G_of (target:'a) : (G&lt;'a&gt;) = { zero = zero_of target one = one_of target two = two_of target three = three_of target negone = negone_of target any = any_of target } let inline factorizeG n = let g = G_of n let rec factorize n j flist = if n = g.one then flist elif n % j = g.zero then factorize (n/j) j (j::flist) else factorize n (j + g.one) (flist) factorize n g.two [] let inline factorize (g:G&lt;'a&gt;) n = //' let rec factorize n j flist = if n = g.one then flist elif n % j = g.zero then factorize (n/j) j (j::flist) else factorize n (j + g.one) (flist) factorize n g.two [] let test1() = let gL = G_of 1L [1L..100000L] |&gt; List.map (fun n -&gt; factorize gL n) let test2() = [1L..100000L] |&gt; List.map (fun n -&gt; factorize (G_of 1L) n) let sw2 = Stopwatch.StartNew() test1() |&gt; ignore sw2.Stop() Console.WriteLine("test1 {0}ms", sw2.ElapsedMilliseconds) let sw = Stopwatch.StartNew() test2() |&gt; ignore sw.Stop() Console.WriteLine("test2 {0}ms", sw.ElapsedMilliseconds) Console.ReadLine() |&gt; ignore </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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