为什么在IDRIS中,“时钟时间”似乎不起作用?

发布于 2025-02-05 06:08:13 字数 1184 浏览 2 评论 0 原文

可能有库可以执行此操作(尽管我还没有找到任何),我实际上是在衡量函数在IDRIS中运行的时间。我发现的方式是使用 ClockTime System 中使用,并在函数运行之前和之后区分。这是一个示例代码:

module Main

import Data.String
import System

factorial : Integer -> Integer
factorial 0 = 1
factorial 1 = 1
factorial n = n * factorial (n - 1)

main : IO ()
main = do
    args <- getArgs

    case args of
        [self ] => putStrLn "Please enter a value"
        [_, ar] => do
            case parseInteger ar of
                Just a => do

                    t1 <- clockTime
                    let r = factorial a
                    t2 <- clockTime

                    let elapsed = (nanoseconds t2) - (nanoseconds t1)

                    putStrLn $ "fact(" ++ show a ++ ") = "
                                       ++ show r ++ " in "
                                       ++ (show elapsed) ++ " ns"

                Nothing => putStrLn "Not a valid number"

为了避免IDRI通过已经评估阶乘来优化程序,我只是要求与参数调用该程序。

但是,该代码不起作用:无论我输入什么数字,例如 10000 ,idris总是返回0纳秒,这使我非常怀疑,甚至只分配一个bigint会花费时间。我使用 iDris main.idr -o main 编译。

我在代码中做错了什么? 时钟时间不是基准的好计划吗?

There are probably libraries to do this (although I haven't found any), I'm actually looking to measure the time a function takes to run in Idris. The way I've found is by using clockTime from System and differentiating between before and after a function runs. Here is an example code:

module Main

import Data.String
import System

factorial : Integer -> Integer
factorial 0 = 1
factorial 1 = 1
factorial n = n * factorial (n - 1)

main : IO ()
main = do
    args <- getArgs

    case args of
        [self ] => putStrLn "Please enter a value"
        [_, ar] => do
            case parseInteger ar of
                Just a => do

                    t1 <- clockTime
                    let r = factorial a
                    t2 <- clockTime

                    let elapsed = (nanoseconds t2) - (nanoseconds t1)

                    putStrLn $ "fact(" ++ show a ++ ") = "
                                       ++ show r ++ " in "
                                       ++ (show elapsed) ++ " ns"

                Nothing => putStrLn "Not a valid number"

To avoid Idris optimising the program by already evaluating the factorial, I just asked that the program be called with an argument.

This code doesn't work though: no matter what numbers I enter, such as 10000, Idris always returns 0 nanoseconds, which makes me quite sceptical, even just allocating a bigint takes time. I compile with idris main.idr -o main.

What am I doing wrong in my code? Is clockTime not a good plan for benchmarks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2025-02-12 06:08:13

IDRIS 1不再维护。
在IDRIS 2中,可以使用。

clockTime : (typ : ClockType) -> IO (clockTimeReturnType typ)

可以在IDRIS2编译器中找到其用于基准测试的一个示例l120“ rel =“ nofollow noreferrer”>在这里。

Idris 1 is no longer being maintained.
In Idris 2, clockTime can be used.

clockTime : (typ : ClockType) -> IO (clockTimeReturnType typ)

An example of its use for benchmarking can be found within the Idris2 compiler, here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文