Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>What's likely to be the source of the problem? I have a few guesses (in decreasing order of likelihood):</p> </blockquote> <ul> <li>Formatting each Int32 with my code is inefficient.</li> </ul> <p>So-so. I'm not sure how well GHC optimises <code>shift x (-24)</code>, maybe using <code>shiftR x 24</code> is better, but I need to look at the generated core to know. Edit: perfectly fine, produces <code>uncheckedIShiftRA#</code>, as one could hope for.</p> <ul> <li>mapM_ over the array with (!) inefficient.</li> </ul> <p>Yes, a bit. Using <code>elems</code> or <code>unsafeAt</code> is slightly faster due to the omission of bounds-checks.</p> <ul> <li>Calling hPutStr a million times is inefficient.</li> </ul> <p>Definitely. But has less impact than I thought.</p> <ul> <li>1 minute to print 1 million values actually is efficient.</li> </ul> <p>No:</p> <pre><code>Prelude&gt; writeFile "IntList.txt" $ unlines . map show $ [1 :: Int .. 1048576] (0.37 secs, 836888976 bytes) </code></pre> <p>It's far more efficient - and may well be more efficient than using an intermediate <code>ByteString</code> - to produce one long <code>String</code> which is printed to the handle at once. The String will be lazily generated, so it will not use much memory, and the generating of the <code>String</code> is pretty efficient (still tweakable if need be).</p> <p>Going off to measure now.</p> <p>Using</p> <pre><code>printArray handle frozenArray = hPutStr handle $ showit (elems frozenArray) where showit :: [Int32] -&gt; String showit (n:ns) = (' ' :) . shows ((n `shiftR` 24) .&amp;. 255) . (' ' :) . shows ((n `shiftR` 16) .&amp;. 255) . (' ' :) . shows ((n `shiftR` 8) .&amp;. 255) . (' ' :) . shows (n .&amp;. 255) $ showit ns showit [] = "" </code></pre> <p>to print the values brought the time to process <code>[0 .. 1048575]</code> from 0.9 seconds to 0.4 seconds. Allocation went down from 2,031,716,528 bytes to 779,086,856 bytes, but memory usage went up from 6MB to 11MB and bytes copied during GC from ~1M to ~5.5M, however, restricting the heap size <code>-M11M</code> to force earlier GC brought memory back down to 6MB and bytes copied during GC to 676,744. (ghc-7.2.2)</p>
    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. This table or related slice is empty.
    1. 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