Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:24:00+00:00 2026-05-31T15:24:00+00:00

I was going to test naive bayes classification. One part of it was going

  • 0

I was going to test naive bayes classification. One part of it was going to be building a histogram of the training data. The problem is, I am using a large training data, the haskell-cafe mailing list since a couple of years back, and there are over 20k files in the folder.

It takes a while over two minutes to create the histogram with python, and a little over 8 minutes with haskell. I’m using Data.Map (insertWith’), enumerators and text. What else can I do to speed up the program?

Haskell:

import qualified Data.Text as T
import qualified Data.Text.IO as TI
import System.Directory
import Control.Applicative
import Control.Monad (filterM, foldM)
import System.FilePath.Posix ((</>))
import qualified Data.Map as M
import Data.Map (Map)
import Data.List (foldl')
import Control.Exception.Base (bracket)
import System.IO (Handle, openFile, hClose, hSetEncoding, IOMode(ReadMode), latin1)
import qualified Data.Enumerator as E
import Data.Enumerator (($$), (>==>), (<==<), (==<<), (>>==), ($=), (=$))
import qualified Data.Enumerator.List as EL
import qualified Data.Enumerator.Text as ET



withFile' ::  (Handle -> IO c) -> FilePath -> IO c
withFile' f fp = do
  bracket
    (do
      h ← openFile fp ReadMode
      hSetEncoding h latin1
      return h)
    hClose
    (f)

buildClassHistogram c = do
  files ← filterM doesFileExist =<< map (c </> ) <$> getDirectoryContents c
  foldM fileHistogram M.empty files

fileHistogram m file = withFile' (λh → E.run_ $ enumHist h) file
  where
    enumHist h = ET.enumHandle h $$ EL.fold (λm' l → foldl' (λm'' w → M.insertWith' (const (+1)) w 1 m'') m' $ T.words l) m

Python:

for filename in listdir(root):
    filepath = root + "/" + filename
    # print(filepath)
    fp = open(filepath, "r", encoding="latin-1")
    for word in fp.read().split():
        if word in histogram:
            histogram[word] = histogram[word]+1
        else:
            histogram[word] = 1

Edit: Added imports

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:24:02+00:00Added an answer on May 31, 2026 at 3:24 pm

    You could try using imperative hash maps from the hashtables package: http://hackage.haskell.org/package/hashtables
    I remember I once got a moderate speedup compared to Data.Map. I wouldn’t expect anything spectacular though.

    UPDATE

    I simplified your python code so I could test it on a single big file (100 million lines):

    import sys
    histogram={}
    for word in sys.stdin.readlines():
        if word in histogram:
            histogram[word] = histogram[word]+1
        else:
            histogram[word] = 1
    print histogram.get("the")
    

    Takes 6.06 seconds

    Haskell translation using hashtables:

    {-# LANGUAGE OverloadedStrings #-}
    import qualified Data.ByteString.Char8 as T
    import  qualified Data.HashTable.IO as HT
    main = do
      ls <- T.lines `fmap` T.getContents
      h <- HT.new :: IO (HT.BasicHashTable T.ByteString Int)
      flip mapM_ ls $ \w -> do
        r <- HT.lookup h w 
        case r of 
          Nothing -> HT.insert h w (1::Int)
          Just c  -> HT.insert h w (c+1)
      HT.lookup h "the" >>= print 
    

    Run with a large allocation area: histogram +RTS -A500M
    Takes 9.3 seconds, with 2.4% GC. Still quite a bit slower than Python but not too bad.

    According to the GHC user guide, you can change the RTS options while compiling:

    GHC lets you change the default RTS options for a program at compile
    time, using the -with-rtsopts flag (Section 4.12.6, “Options affecting
    linking”). A common use for this is to give your program a default
    heap and/or stack size that is greater than the default. For example,
    to set -H128m -K64m, link with -with-rtsopts=”-H128m -K64m”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Childbrowser. My case: haveing an index.html file and test.html. going to test.html start
I have a set of test accounts that are going to be created but
I am new in MVC3 i am going to create a MVC3 test project
I realize I'm going to get flamed for not simply writing a test myself...
I'm writing a selenium grid test suite which is going to be run on
<div> <div>test</div> </div> $(div:contains('test')).css('display','none'); I know I am going to kick myself on this.
I am using Eclipse Indigo. Not sure what is going on, but what I
Okay, so I have a client/server test going on, and I am passing the
I going to test the class method which get the integer as parameter: def
I have created a simple project in java using eclipse ide to test the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.