How to find the number of cores at runtime in Haskell

前端 未结 5 1742
野趣味
野趣味 2021-02-13 06:22

Does Haskell have a method for determining the number of CPU cores present on a machine at runtime?

5条回答
  •  [愿得一人]
    2021-02-13 06:32

    You could copy'n'paste this code into a file called numCores and compile it with your Haskell code. Than you can use the FFI to import its definition and use it directly in your Haskell code:

    {-# LANGUAGE ForeignFunctionInterface #-}
    import Control.Applicative ((<$>))
    import Foreign.C.Types (CInt)
    
    foreign import ccall "getNumCores" c_getNumCores :: IO CInt
    getNumCores :: IO Int
    getNumCores = fromEnum <$> c_getNumCores
    

提交回复
热议问题