logo
down
shadow

Why is my haskell code so slow


Why is my haskell code so slow

By : Андрей Куприёв
Date : November 17 2020, 11:55 AM
Does that help use a Session to make sure Wreq reuses connections across your requests. See multiple requests for an example.
code :


Share : facebook icon twitter icon
Abnormally slow Haskell code

Abnormally slow Haskell code


By : Janas Khan
Date : March 29 2020, 07:55 AM
may help you . If you're patient, you'll notice that the second result is calculated much faster than the first. That's because your implementation takes some time to read in the csv files.
You may be tempted to stick a print statement to see when it's done loading like so:
code :
main = do
    trainingSample <- readDigits "trainingsample.csv"
    validationSample <- readDigits "validationsample.csv"
    putStrLn "done loading data"
readDigits :: String -> IO [Digit]
readDigits filename = do
    fileContent <- readFile filename
    putStr' $ filename ++ ": "
    rows <- forM (tail $ lines fileContent) $ \line -> do
      let xs = parseDigit line
      putStr' $ case compare (sum $ pixels xs) 0 of
                LT -> "-"
                EQ -> "0"
                GT -> "+"
      return xs
    putStrLn ""
    return rows
  where putStr' s = putStr s >> hFlush stdout
import qualified Data.Vector.Unboxed as U
-- ...

type Pixels = U.Vector Int
-- ...

distance :: Pixels -> Pixels -> Float
distance d1 d2 = sqrt . U.sum $ U.zipWith pointDistance d1 d2
    where pointDistance a b = fromIntegral $ (a - b) * (a - b)

parseDigit :: T.Text -> Digit
parseDigit s = Digit label (U.fromList pixels)
    where (label:pixels) = map toDigit $ T.splitOn (T.pack ",") s
          toDigit s      = either (\_ -> 0) fst (T.Read.decimal s)
identify :: Digit -> V.Vector Digit -> (Digit, Float)
identify digit training = V.minimumBy (comparing snd) distances
    where distances = V.map fn training
          fn ref    = let d = distance (pixels digit) (pixels ref) in d `seq` (ref, d)
% ghc --make -O2 Main.hs
[1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking Main ...
% time ./Main
./Main  5.00s user 0.11s system 99% cpu 5.115 total
Slow Haskell and Python code with Project Euler #2

Slow Haskell and Python code with Project Euler #2


By : zetwon
Date : March 29 2020, 07:55 AM
help you fix your problem Your Haskell implementation is simply wrong: it never completes because:
code :
fib n = (fib n - 1) + (fib n - 2)
fib n = (fib n) - 1 + ((fib n) - 2)
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

genFibs n maxVal
  | fib n < maxVal = fib n : genFibs (n+1) maxVal
  | otherwise = []

totalSum = sum $ filter even $ genFibs 1 4000000

main = print totalSum
$time ./fibo 
4613732

real    0m1.334s
user    0m1.324s
sys     0m0.009s
def fibo(n):
    prev, cur = 0, 1
    for i in range(n):
        prev, cur = cur, prev + cur
    return cur
In [8]: %%timeit
   ...: tot = 0
   ...: n = 0
   ...: while True:
   ...:     num = fibo(n)
   ...:     if num > 4000000:
   ...:         break
   ...:     elif num % 2 == 0:
   ...:         tot += num
   ...:     n += 1
   ...:     
10000 loops, best of 3: 54.8 µs per loop
Why is this Haskell code so slow?

Why is this Haskell code so slow?


By : Ameya Kharkar
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm kind of new to Haskell and tried making a scrabble solver. It takes in the letters you currently have, finds all permutations of them and filters out those that are dictionary words. The code's pretty simple: ,
I'm kind of new to Haskell and tried making a scrabble solver.
code :
import Data.List
import qualified Data.Map.Strict as Map
import Control.Monad
import System.IO

main = do
  contents <- readFile "words"
  let pairs = [ (sort w, [w]) | w <- words contents ]
      dict = foldl' (\m (k,v) -> Map.insertWith (++) k v m) Map.empty pairs
      -- dict = foldr (\(k,v) m -> Map.insertWith (++) k v m) Map.empty pairs
  forever $ do
    putStr "Enter letters: " >> hFlush stdout
    letters <- getLine
    case Map.lookup (sort letters) dict of
      Nothing -> putStrLn "No words."
      Just ws -> putStrLn $ "Words: " ++ show ws
steer rat tuna lapse groan neat
Haskell: Code running too slow

Haskell: Code running too slow


By : Justin D
Date : March 29 2020, 07:55 AM
Hope that helps There is a standard trick for computing the Fibonacci numbers that can easily be adapted to your problem. The naive definition for Fibonacci numbers is:
Why My Haskell Code is so slow compare to Swift and C

Why My Haskell Code is so slow compare to Swift and C


By : Pacha Munikrishna
Date : March 29 2020, 07:55 AM
I wish this help you I measure the Haskell code running in 0.03 seconds, leading me to believe you have ran this in the interpreter (which is for development, not performant execution) instead of compiling the code. Also, you probably let the type default to Integer instead of using machine Int values.
code :
tommd@HalfAndHalf /tmp% ghc -O2 t.hs && time ./t >/dev/null
[1 of 1] Compiling Main             ( t.hs, t.o )
Linking t ...
./t > /dev/null  0.03s user 0.00s system 87% cpu 0.040 total
tommd@HalfAndHalf /tmp% cat t.hs

main :: IO ()
main = do
  let l = [1..200] :: [Int]
  let pythagoras = [ x | x <- l, y <- l, z <- l, x^2 == y^2 + z^2]
  print pythagoras
Related Posts Related Posts :
  • Haskell Shelly sample code
  • Finding type signatures in Haskell that are too specific
  • haskell - will pattern matching work faster for non-specific form of an algebraic data type?
  • Cannot enter multiline statements in GHCi
  • Using Gloss to run a simulation while using SDL to play a sound
  • Haskell - Calculating the shortest path using trees
  • Beginner: Converting Types in Haskell
  • G-machine, (non-)strict contexts - why case expressions need special treatment
  • Indentation with Let Statements
  • Haskell parse list of integers with Attoparsec
  • Change (0, 1] to (0, 1) without branching
  • Haskell equivalent to contains
  • Is there a way to unmap in Haskell?
  • Using HLint.hs file in EclipseFP
  • Don't know how to use . and $ operator in Haskell
  • How is FRP handled in terms of memory?
  • Generalizing traversal of expressions with changes on specific nodes
  • Why does pattern matching on this GADT seem to introduce ambiguity in the type checker?
  • How to detect end of input with pipes
  • Basic Haskell IO Monad FilePath join
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org