Haskell: fastest way to read and print a file? & hGetBuf example?

I was playing around with simple IO in Haskell just for the sake of learning and decided to implement a very basic version of "cat" for a rough speed comparison. Being a Haskell noob, I tried various combinations of readFile and hGetContents from System.IO before I realized that they read a single Char at a time, which explained why there were slower than even Python and Perl. I found the hGetBuf function but I couldn't figure out how to get my data back from the Ptr. Eventually I ended up with this:
import System( getArgs )
import qualified Data.ByteString as B
main = getArgs >>= (B.readFile.head) >>= B.putStr
time cat /var/log/pacman.log | wc
297783 1945570 32536814
real 0m0.624s
user 0m0.620s
sys 0m0.017s
time ./test /var/log/pacman.log | wc
297783 1945570 32536814
real 0m0.671s
user 0m0.640s
sys 0m0.040s
where "test" is the program above compiled with "ghc --make test.hs -o test -O".
So, my questions:
1) Is there any way to shave off the last few milliseconds to make it as fast as cat for that operation?
2) Can someone give a clear example of how to use hGetBuf, specifically how to "marshall" a Ptr into a String or ByteString?
I've tried to find examples but the very few that I've found haven't been that helpful.
Slightly off-topic yet tangential questions:
*) Does anyone else find it irksome that you have to dig through Haskell's abstraction to deal with the underlying system? I'm only just beginning to learn it but I've already come across a few things which feel like square blocks hammered into round holes.

@CBM80
Thanks for the link. I've bookmarked it for now.
@brisbin33
I've been coding a bit in Haskell lately and it feels like I'm getting comfortable with it, including monads (e.g. state transformers, system IO). I think I'm over the first big conceptual stumbling block and it's starting to feel much more intuitive. Nevertheless I still generally agree with that statement, although now I would have worded it differently.
It feels like they've pulled a sheet over the underlying system. You can still see the basic shape of it and where different bits protrude, but you're supposed to pretend that it's all nice and smooth. If you want to do anything serious with it, you have to cut a hole through the sheet (i.e. use the foreign function interface and another language which can get to it) to gain full access.
When I wrote that I think I actually had Ints and Integers in mind. The difference between them is only how the underlying system represents them. From a purely abstract|functional perspective, there should be no distinction between them. I understand that the language is ultimately bound by the hardware and I actually appreciate that the distinction can enable the programmer to optimize his code. It just annoys me that there is a layer of abstraction that prevents me from exerting more control over the types. For example, it would be nice to be able to declare my own "PositiveInt" type and have it represented by an unsigned int in the underlying system. I can "see" from within Haskell how "Int" is represented, but I can't create a similar representation (without resorting to the FFI, afaik). There has been talk about this before and how to make all types user-definable. Some tutorial even give the dummy declaration of Int as "data Int = ...|-3|-2|-1|0|1|2|...". Obviously you would need some sort of meta-language to handle it though (which seems to be in the works).
For the record, I think the FFI is great and I see great potential in it and can't wait to start using it.
At some point I solved some of the problems on Project Euler (and got sidetracked with Perl and Python solutions). My quest to learn Haskell has also been periodic and so far I haven't gone back to PE but I intend to. Btw, I found it insightful to compare my solutions to the solutions posted in the wiki. They're full of little epiphanies.
*edit*
I realize that many apparent limitations are likely due to my own ignorance. I'm reading through the pages posted by CBM80 and already see some ways to get around the abstraction.
Last edited by Xyne (2010-05-06 19:03:17)

Similar Messages

  • Fastest way to read and write to/from muliple DAQ channels

    I'm developing an application in which multiple DAQ channels will be monitored, for example, displacement, load, strain, temperature and so on. My question is what is the fastest and most efficent way to check the value of these inputs, compare them to user inputs and then decide whether or not to write a different voltage to an output. I'm using LV 7.1 on Win 2000.

    I guess my first question would be are all these channels going to be on one card? Are you going to just make single measurements (are you interested in instantaneous values vs. "waveforms")? Doing a "Sample channels" for instance will make a single measurement of several channels (on the same board) and present you with an array of the resulting data, one element per channel. How fast do you need to sample, are all of the signals going to be on the same board? The latest NI-Daq allows configuring tasks that mix channel types (T'couples, straight voltage measurements, etc.) which makes configuring the DAQ easier.
    If you can give us a little more information regarding what you are trying to accomplish it will help.
    P.M.
    Putnam
    Certified LabVIEW Developer
    Senior Test Engineer
    Currently using LV 6.1-LabVIEW 2012, RT8.5
    LabVIEW Champion

  • Fast way to read and write console

    hi guys
    what's the fastest way to read and write from console?
    For writing I'm using (I think thats the fastest way)
    System.out.println(foo)and I have to read the following - all numbers are ints (that means m + n + 1 lines):
    1 * "<n> <m>"
    n * "<a> <b> <c>"
    m * "<a> <b>"Actual I'm reading (the second line for example) with
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] items = br.readLine().split("[^\\-\\.0-9]+");
    Target mainTarget = new Target(Integer.parseInt(items[0]), Integer.parseInt(items[1]), Integer.parseInt(items[2]));
    for (int i = 1; i < m; i++) {
        items = br.readLine().split("[^\\-\\.0-9]+");
        mainTarget.addTarget(Integer.parseInt(items[0]), Integer.parseInt(items[1]), Integer.parseInt(items[2]));
    } But that isn't really fast...
    have you any idea?
    grz faetzminator

    faetzminator wrote:
    right, I have 1 + 1 + 1 =3 up to 10^6-1 + 10^6-1 + 1 =2*10^6-1 input lines with max 4999997 integers (and I have to store 2999999 of them)
    ok, if that is the fastest way... (maybe the reading of the input file is so slow; I run eg "java Foo < input > output")All those br.readLine calls where you never check if the return is null are dangerous.
    I really don't understand what you are doing. You mentioned System.in at first and now reading from files.
    I also am pretty sure that you are in fact running out of memory.

  • How to read and write Xml file at client side using JavaScript !

    Hello,
    i am new to javascript.
    I have requirement to read and update XML file at client side.
    Will you please guide what could be the best way to read and update XMl file using javascript.
    Thanks,
    Zuned

    This is a Java forum,not a Javascript forum. Maybe you should ask here [http://www.webdeveloper.com/forum/forumdisplay.php?forumid=3&s|http://www.webdeveloper.com/forum/forumdisplay.php?forumid=3&s].

  • Is there a way to export and print the browser data as a spread sheet?

    I'm in the process of logging approximately 1,500 shots for a documentary (the old fashioned way - long hand: shot #, TC in, descriptions... ) and I'm wondering if there's a way to export and print the data in the browser as a spread sheet.  It seems like a pretty simple thing, but I don't see it any where. Am I missing something?

    Yes, people I work with use it, assistants fill the fields out for me. As far as I know there is no "Print Browser" option. Some folks have assistants copy and paste the info into text, csv or xml files.    Since everthing has gone to meta tags, this approach is going the way of the electric spoon or feeding mayonnaise to Tuna.
    Peace & Covering Fire,
    B.S.

  • We are trying to implement a process so that any document that needs to be printed through our Java application will be printed as PDF using Adobe Reader. For which, We created and execute the below command line to call Adobe Reader and print the PDF on a

    We are trying to implement a process so that any document that needs to be printed through our Java application will be printed as PDF using Adobe Reader. For which, We created and execute the below command line to call Adobe Reader and print the PDF on a printer."C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /T "\\<Application Server>\Report\<TEST.PDF>" "<Printer Name>". Current Situation: The above command line parameter when executed is working as expected in a User's Workspace. When executed in a command line on the Application Server is working as expected. But, the same is not working while executing it from Deployed environment.Software being used: 1. Adobe 11.0 enterprise version. 2. Webshpere Application Server 8.5.5.2. Please let us know if there is a way to enable trace logs in Adobe Reader to further diagnose this issue.

    This is the Acrobat.com forum.  Your question will have a much better chance being addressed in the Acrobat SDK forum.

  • IMovie: whats the fastest way to import and save all original media?

    Good afternoon. Ok I need help please. Is there a way to simply take all the imported original media and just save it all in one fell swoop? Rather than having to import and then process/export clip by clip.
    I thought I had a quicker way just grabbing all the original media from the imported camera (right clicking imovie library > show package contents > original media) untill I tried to play one of the original media files - no sound and it was mov file. I assume mp4 is better???
    I have 2800 hours of video I need to import off my camera. I don't need to modify them at all, aside from the occasional combining of clips into one movie. So I need the fastest way. Importing and then exporting clip by clip will take me forever. I would prefer to just import and then just drag all 50 clips or so into my movies folder. But as stated above that does not seem to work as expected unless I'm missing a step.
    Thank you!

    ps Can I take the original media files (mov format) and batch convert them using handbrake or MPEG streamclip? And if so would the resulting videos then have sound? And which is the best format (container codec whatever) to export them to? Thanks!

  • Read and print PDF from content server

    Hi,
    I have a PDF document stored in the content server, I have a requirement to schedule a job to read the documents and print them. Is it possible to read and print them directly ?
    Thanks in advance

    Go Thru these links.
    Hope it will help you.
    http://help.sap.com/saphelp_nw04/helpdata/en/c8/4adf7ba13c4ac1b4600d4df15f8b84/content.htm
    http://help.sap.com/saphelp_nw2004s/helpdata/en/37/47a2be350c4ac8afe36b691203971f/frameset.htm
    Reward if help.

  • HT1391 How can I read and print digital booklets that are downloaded togheter with the recordings I purchased

    How can I read and print digital booklets that are downloaded togheter with the albuns I bought?

    cu is right.
    classloader returns the URL of your resource. You can the jar using something like the following...
    ClassLoader classloader = this.getClassLoader();
    url = classloader.getResource("your_resouces...");
    if(url == null)
    System.out.println("cannot find resource ");
    return null;
    String jar = url.getFile();
    jar = jar.substring(jar.indexOf("/")+1, jar.lastIndexOf("!")).replace('/', '\\');
    obviously, this code snippet works on windows only. Now you have the jar file path. I use Jar file classes to access the jar file. (e.g. you can search for each class files)
    Ming

  • Open and print multiple files from the Finder all at once.

    I'm trying to research how to do this. In OS9 you used to be able to hi-lite multiple files (Word files for example) and just hit Command+P. This would automatically open all of the files AND print them.
    I can't seem to do this in my OS X. Or can I? I can hi-lite and go to File/Print, but the computer is only opening but not printing.
    Any suggestions??
    iMac   Mac OS X (10.4.8)  

    I don't know how to both "open and print" multiple files in one step. The ⌘O combination of course will open multiple items. The items remain selected so switching back to the "Finder" and dragging multiple icons onto the icon for "/Applications" > "Utilities" > "Printer Setup Utility.app" seems to cause them all to be printed to the default printer.
    Alternatively, a user's "~/Library/Printers" folder contains printer icons -- I'm not sure what they are exactly, but they represent the printers set up for that account. They function pretty much like the pre-OS X "Desktop Printer" icons so dragging and dropping multiple files onto them should cause them to be printed. It may even be possible to create aliases to them on the "Desktop" to recreate "Desktop Printer" functionality. But again, this won't open the files into their applications.

  • How do i view and print pdf file in mac osx with ff4.0?

    How do i view and print pdf files in ff 4.0 on a mac with osx. for some reason the updated ff version does not support it. Makes no sense that something that worked in an earlier version now does not...
    Thanks,
    Ron

    Please don't post the same question multiple times!

  • How can I open and print PDF files?

    How can I open and print PDF files?

    With the free Reader if they are not print protected. With Acrobat otherwise.

  • How can I read the printer snapshot file printers.xml

    How can I read the printer snapshot file printers.xml

    XML files are typically viewed using a web browser.
    Open the XML file and the default program should open it automatically.  If you are prompted to select a program, use the option that lets you choose a program and brows through the directories for your browser (for example, use Internet Explorer, Firefox, etc.).
    If need be, you should be able to right-click the file and select open with to choose the program to open it with.
    There are other XML viewers out ther, but web browsers are typically the default as XML is a web language.
    ↙-----------How do I give Kudos?| How do I mark a post as Solved? ----------------↓

  • What is the easiest way to read fixed width ascii files?

    There does not seem to be an easy way to read fixed with ascii files in labview.  CSV files and other delimited files are easy, but I cannot think of a simple way to read data from a fixed width ascii file.

    jgkempen wrote:
    There does not seem to be an easy way to read fixed with ascii files in labview. 
    Reading a file does not depend on its contents. Unless the file is gigantic, you can just read it as one big string and parse it later, especiall if you want the entire contents of the file.
     If you only need a subset, you can calculate the offsets and sizes as multiples of your record size. That's the beauty of fixed size records.
    It is typically not efficient to read a file in a loop, small portions at a time, so I would not recommend that except in some very specific situations.
    Could you be a bit more specific on what exactly you need to do? Is your problem with reading or with parsing the fixed sized records? Could you attach a sample file and some description on what you need to read from it.
    Message Edited by altenbach on 09-05-2007 07:50 PM
    LabVIEW Champion . Do more with less code and in less time .

  • I'm trying to share folder between users on a single mac.  I put the folder in "shared," set permissions so other user can read and write, enabled file sharing, but can't find the folder on the second user's account.  Any help?

    I'm trying to share a folder between users on a single mac.  I want both users to be able to read and write so the folder stays current on both accounts.  I put the folder in "shared," set permissions on folder so other user can read and write, enabled file sharing, but can't find the folder on the second user's account.  Any help?

    Did you log out of one account and into the other or just used Fast user switching?
    Is the permissions set to anyone?
    When you move data to teh Shared folder is it copied or just moved?
    If copied then it's not a folder both can access, just a way station like a USB thumb drive that things are coped too and off of likely.
    You can run this #5 on each user account to reset the user permissions once they are taken back out of the Shared folder
    Step by Step to fix your Mac

Maybe you are looking for

  • Some of my iphone apps do not show up in my iphone after sync

    There are some apps that do not show up after i click the sync button. At the top of the itunes window where it shows the status, it says syncing iphone then it installs a 50 mb app in 0.5 seconds and then says sync again, then sync complete, ok to d

  • ATP control in ABAP code, product allocation

    Hello, Scenario is like this: I am creating sales order for 10 PC of material. Material is fully available on unrestricted stock but product allocation is allowing me to use only 3 PC of material. Question: I am searching for ABAP code which tells me

  • Landscape keyboard in App Store

    Is there a hidden feature that lets you use a landscape keyboard when writing reviews in the App Store?

  • How to know the process chain start time and end time

    Hi Experts,                How to know the process chain start time  and end time . Thanks in advance Regards Gutti Edited by: guttireddy on Feb 23, 2012 11:30 PM

  • Can't Import DualDisc on G5

    Hi, Can't seem to import (the sound is lousy with buzzing noise throughout songs) a DualDisc CD. It imports fine on my girlfriends ibook. Do I need a software upgrade? Thanks for any help.