[Haskell-cafe] Slow Text.JSON parser

Levi Greenspan greenspan.levi at googlemail.com
Tue Jan 13 07:16:46 EST 2009


Dear list members,

I tried Text.JSON from hackage and did an initial test to see how well
it performs. I created a single JSON file of roughly 6 MB containing a
single JSON array with 30906 JSON objects and used the following code
to parse it:


module Main where

import System.IO
import Data.Time.Clock
import System.Environment
import Text.Printf
import Text.JSON

parse s = do
    start <- getCurrentTime
    let !len = decode s
    end <- getCurrentTime
    print len
    printf "Elapsed time = %s\n" (show $ diffUTCTime end start)
    where
        decode s = case decodeStrict s of
                        Ok (JSArray a) -> length a
                        _ -> -1

main = do
    file <- getArgs >>= return . head
    withFile file ReadMode (\h -> hGetContents h >>= parse)



The outcome was something like:

30906
Elapsed time = 2.902755s

on my 2GHz core 2 duo.

Another Java-based JSON parser (Jackson:
http://www.cowtowncoder.com/hatchery/jackson/index.html) gives me:

30906
Elapsed time = 480 ms

Now I wonder why Text.JSON is so slow in comparison and what can be
done about it. Any ideas? Or is the test case invalid?

Thanks,
Levi

-----------------------------------
The Java code for the Jackson test is:

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.map.JsonTypeMapper;
import org.codehaus.jackson.map.JsonNode;

import java.io.File;

class Test {

    public static void main(String[] args) throws Exception {
        final long start = System.currentTimeMillis();
        final JsonTypeMapper mapper = new JsonTypeMapper();
        final JsonParser parser = new
JsonFactory().createJsonParser(new File(args[0]));
        final JsonNode root = mapper.read(parser);
        final long end = System.currentTimeMillis();
        System.out.println(root.size());
        System.out.println(String.format("Elapsed time = %d ms", end - start));
    }
}


More information about the Haskell-Cafe mailing list