Difference between revisions of "Performance/IO"

From HaskellWiki
Jump to navigation Jump to search
(hGetBuf, and mmapped IO are options)
 
(Updated with iteratees)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{Performance infobox}}
'''IO'''
 
  +
[[Category:Performance|IO]]
   
  +
Before continuing, I strongly recommend reading [[Performance/Strings]] first. Fast I/O and string handling overlap in many places, so this page will only discuss the parts that are not specific to strings.
If the standard lazy IO operations are proving to be a bottleneck,
 
  +
buffer-based IO is an alternative (hGetBuf/hPutBuf). This can be
 
  +
== Iteratee I/O ==
particularly effective when combined with packed strings.
 
  +
  +
[[Iteratee I/O|Iteratees]] are a relatively new approach to streaming I/O. If your code uses I/O extensively (for example, in a web server), iteratees are the best way to do it.
  +
  +
Using the [http://hackage.haskell.org/package/conduit Conduit] library, a program to read a file and pipe it to stdout can be written as follows:
  +
  +
<haskell>
  +
main = runResourceT (sourceFile "test.txt" $$ sinkHandle stdout)
  +
</haskell>
  +
  +
== Other solutions ==
  +
  +
If this is too high-level for you,
 
buffer-based IO is an alternative (<code>hGetBuf</code>/<code>hPutBuf</code>). This can be
 
particularly effective when combined with packed strings (see [[wc]]).
   
 
Some external libraries also provide memory mapped IO.
 
Some external libraries also provide memory mapped IO.
  +
  +
In 2006, someone came up with a solution called [[Library/Streams|Streams]]. However, it does not seem to be maintained anymore.

Latest revision as of 02:13, 12 July 2012

Haskell Performance Resource

Constructs:
Data Types - Functions
Overloading - FFI - Arrays
Strings - Integers - I/O
Floating point - Concurrency
Modules - Monads

Techniques:
Strictness - Laziness
Avoiding space leaks
Accumulating parameter

Implementation-Specific:
GHC - nhc98 - Hugs
Yhc - JHC

Before continuing, I strongly recommend reading Performance/Strings first. Fast I/O and string handling overlap in many places, so this page will only discuss the parts that are not specific to strings.

Iteratee I/O

Iteratees are a relatively new approach to streaming I/O. If your code uses I/O extensively (for example, in a web server), iteratees are the best way to do it.

Using the Conduit library, a program to read a file and pipe it to stdout can be written as follows:

main = runResourceT (sourceFile "test.txt" $$ sinkHandle stdout)

Other solutions

If this is too high-level for you, buffer-based IO is an alternative (hGetBuf/hPutBuf). This can be particularly effective when combined with packed strings (see wc).

Some external libraries also provide memory mapped IO.

In 2006, someone came up with a solution called Streams. However, it does not seem to be maintained anymore.