Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my code using monadic lists from the List package so slow?
    primarykey
    data
    text
    <p>Last week user Masse asked a <a href="https://stackoverflow.com/questions/3881835/how-to-list-directories-faster">question about recursively listing files</a> in a directory in Haskell. My first thought was to try using monadic lists from the <a href="http://hackage.haskell.org/package/List" rel="nofollow noreferrer"><code>List</code> package</a> to avoid building the entire list in memory before the printing can start. I implemented this as follows:</p> <pre><code>module Main where import Prelude hiding (filter) import Control.Applicative ((&lt;$&gt;)) import Control.Monad (join) import Control.Monad.IO.Class (liftIO) import Control.Monad.ListT (ListT) import Data.List.Class (cons, execute, filter, fromList, mapL) import System (getArgs) import System.Directory (getDirectoryContents, doesDirectoryExist) import System.FilePath ((&lt;/&gt;)) main = execute . mapL putStrLn . listFiles =&lt;&lt; head &lt;$&gt; getArgs listFiles :: FilePath -&gt; ListT IO FilePath listFiles path = liftIO (doesDirectoryExist path) &gt;&gt;= listIfDir where valid "." = False valid ".." = False valid _ = True listIfDir False = return path listIfDir True = cons path $ join $ listFiles &lt;$&gt; (path &lt;/&gt;) &lt;$&gt; (filter valid =&lt;&lt; fromList &lt;$&gt; liftIO (getDirectoryContents path)) </code></pre> <p>This works beautifully in that it starts printing immediately and uses very little memory. Unfortunately it's also dozens of times slower than a comparable <code>FilePath -&gt; IO [FilePath]</code> version.</p> <p>What am I doing wrong? I've never used the <code>List</code> package's <code>ListT</code> outside of toy examples like this, so I don't know what kind of performance to expect, but 30 seconds (vs. a fraction of a second) to process a directory with ~40,000 files seems much too slow.</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.
 

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