gpt4 book ai didi

performance - 是否有适合脚本的快速启动 Haskell 解释器?

转载 作者:行者123 更新时间:2023-12-03 13:12:38 25 4
gpt4 key购买 nike

有谁知道适合用于编写 shell 脚本的快速启动的 Haskell 解释器?在我的旧笔记本电脑上使用 Hugs 运行“hello world”需要 400 毫秒,而在我当前的 Thinkpad X300 上需要 300 毫秒。这对于瞬时响应来说太慢了。使用 GHCi 的时间类似。

函数式语言不必很慢:Objective Caml 和 Moscow ML 都在 1 毫秒或更短的时间内运行 hello world。

澄清 : 我是 GHC 的重度用户,我知道如何使用 GHCi。我知道所有关于编译以快速完成任务的知识。解析成本应该完全不相关:如果 ML 和 OCaml 的启动速度可以比 GHCi 快 300 倍,那么还有改进的空间。

我在寻找

  • 脚本编写的便利性:一个源文件,无需二进制代码,相同代码在所有平台上运行
  • 与其他解释器相当的性能,包括快速启动和执行简单程序,如
    module Main where
    main = print 33

  • 我不是在为更严肃的程序寻找编译性能。重点是看看 Haskell 是否对脚本有用。

    最佳答案

    为什么不创建一个脚本前端来编译脚本,如果它以前没有或者编译的版本已经过时了。

    这是基本思想,这段代码可以改进很多——搜索路径而不是假设所有内容都在同一个目录中,更好地处理其他文件扩展名,等等。而且我在 haskell 编码(ghc-compiled-script。 hs):

    import Control.Monad
    import System
    import System.Directory
    import System.IO
    import System.Posix.Files
    import System.Posix.Process
    import System.Process

    getMTime f = getFileStatus f >>= return . modificationTime

    main = do
    scr : args <- getArgs
    let cscr = takeWhile (/= '.') scr

    scrExists <- doesFileExist scr
    cscrExists <- doesFileExist cscr
    compile <- if scrExists && cscrExists
    then do
    scrMTime <- getMTime scr
    cscrMTime <- getMTime cscr
    return $ cscrMTime <= scrMTime
    else
    return True

    when compile $ do
    r <- system $ "ghc --make " ++ scr
    case r of
    ExitFailure i -> do
    hPutStrLn stderr $
    "'ghc --make " ++ scr ++ "' failed: " ++ show i
    exitFailure
    ExitSuccess -> return ()

    executeFile cscr False args Nothing

    现在我们可以创建这样的脚本(hs-echo.hs):
    #! ghc-compiled-script

    import Data.List
    import System
    import System.Environment

    main = do
    args <- getArgs
    putStrLn $ foldl (++) "" $ intersperse " " args

    现在运行它:
    $ time hs-echo.hs "Hello, world\!"     
    [1 of 1] Compiling Main ( hs-echo.hs, hs-echo.o )
    Linking hs-echo ...
    Hello, world!
    hs-echo.hs "Hello, world!" 0.83s user 0.21s system 97% cpu 1.062 total

    $ time hs-echo.hs "Hello, world, again\!"
    Hello, world, again!
    hs-echo.hs "Hello, world, again!" 0.01s user 0.00s system 60% cpu 0.022 total

    关于performance - 是否有适合脚本的快速启动 Haskell 解释器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/712696/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com