
Hi! I want to read a text file from a socket and process it as a String (using Parsec). Is hGetContent ok, or are there mor efficient alternatives available, even if I want a String anyway? Greetings, Carsten -- Carsten Schultz (2:38, 33:47), FB Mathematik, FU Berlin http://carsten.codimi.de/ PGP/GPG key on the pgp.net key servers, fingerprint on my home page.

Carsten Schultz writes:
I want to read a text file from a socket and process it as a String (using Parsec). Is hGetContent ok, or are there mor efficient alternatives available, even if I want a String anyway?
hGetContents is fine, although you have to be aware that the function does not implement any timeouts. Meaning, if you don't receive the data you need, your program will hang. Whether your application is fast or not doesn't really depend on how you read the data, but on how you _process_ it. Personally, I've always tried to stay away from hGetContents and friends, because once you have the "whole" list at your disposal, the temptation is huge to write code that treats the data as if you _had_ it all in memory, and that usually is very inefficient. I'd rather read the input in blocks, or lines, and process it as such. Peter

On Mon, Oct 04, 2004 at 08:34:42PM +0200, Peter Simons wrote:
hGetContents is fine, although you have to be aware that the function does not implement any timeouts. Meaning, if you don't receive the data you need, your program will hang.
Whether your application is fast or not doesn't really depend on how you read the data, but on how you _process_ it.
Simply counting the number of characters in input using getContents can be 6 times slower than doing the same thing using hGetArray (still doing unsafeRead for every character, even if it's not necessary for this particular task). So, hGetContents may not be the best solution if you care about performance, at least in GHC 6.2.1. Best regards, Tom -- .signature: Too many levels of symbolic links
participants (3)
-
Carsten Schultz
-
Peter Simons
-
Tomasz Zielonka