每条规则经过的时间

发布于 2024-11-05 15:44:24 字数 1012 浏览 0 评论 0原文

我有这个 bash 代码:(

在脚本开始时:)

function timer()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s')
    else
        local  stime=$1
        etime=$(date '+%s')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        ds=$((dt % 60))
        dm=$(((dt / 60) % 60))
        dh=$((dt / 3600))
        printf '%d:%02d:%02d' $dh $dm $ds
    fi
}

t=$(timer)

(并且在脚本结束时:)

printf 'Elapsed time: %s\n' $(timer $t)

计算脚本所用的总时间。此代码在 bash(shell)脚本中运行良好。因此,我想将此代码放入每个规则的 makefile 中。

我怎样才能把这个函数放到Makefile中呢?如何在每条规则中调用它们?

我做了这样的事情:

define TIME
     stime=$(1)
     etime=$(date '+%s')
     dt=$((etime - stime)) \
     ds=$((dt % 60)) \
     ...
endef

并且在每条规则中:

rule1: dep1 dep2 dep3
    ...SOME STUFF
    @$(call TIME, starttime)

rule2: depx depD rule1
    ...SOME STUFF
    @$(call TIME, starttime)

但是数学运算不起作用。我尝试了很多事情但我做不到

I have this bash code:

(At the start of the script:)

function timer()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s')
    else
        local  stime=$1
        etime=$(date '+%s')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        ds=$((dt % 60))
        dm=$(((dt / 60) % 60))
        dh=$((dt / 3600))
        printf '%d:%02d:%02d' $dh $dm $ds
    fi
}

t=$(timer)

(and, at the end of the script:)

printf 'Elapsed time: %s\n' $(timer $t)

to calculate the total time elapsed by the script. This code works fine in a bash (shell) script. So, I want to put this code in a makefile for each rule.

How can I put this function in Makefile? And how can call'em in each rule?

I made something like this:

define TIME
     stime=$(1)
     etime=$(date '+%s')
     dt=$((etime - stime)) \
     ds=$((dt % 60)) \
     ...
endef

and in each rule:

rule1: dep1 dep2 dep3
    ...SOME STUFF
    @$(call TIME, starttime)

rule2: depx depD rule1
    ...SOME STUFF
    @$(call TIME, starttime)

but the math operation does not work. I tried a lot of things but I can't do works

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

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

发布评论

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

评论(3

2024-11-12 15:44:24

问题在于,在您的 bash 脚本中,变量 t 从开始(工作之前)到结束(可以从结束时间中减去它)一直存在。在 Make 配方中,每一行都有自己的 shell,因此在前面的行中设置的 shell 变量在后面的行中将不可用。

您可以将配方的所有命令串在一起,这样您就可以在开头设置 t 并在结尾使用它,但这非常笨拙。我建议您将 t 写入文件,也许是 rule1_time,这样对 timer 的两次调用就不需要公共变量。哦,不要尝试在命令中使用 call

STIME = date '+%s' > $@_time
ETIME = read st < $@_time ; echo $((`date '+%s'`-$st))

all:
    $(STIME)
    do stuff
    $(ETIME)

编辑:
我编写上面的代码作为概念证明;我是为了清晰,而不是精致。如果我正确理解您的评论,您现在想知道如何将时间分解为小时、分钟和秒,而不需要从每个规则调用多个函数。有多种方法可以做到这一点,这可能是最干净的:

ETIME = @read st < $@_time ; st=$((`date '+%s'`-$st-68400)) ; echo Elapsed time: `date -d @$st '+%H:%M:%S'`

The trouble is that in your bash script, the variable t survives from the beginning (before the work) to the end (when it can be subtracted from the end time). In a Make recipe, each line has its own shell, so a shell variable set in an early line won't be available in a later line.

You could string all of the commands of a recipe together in one line, so that you can set t at the beginning and use it at the end, but that's pretty clumsy. I'd suggest you write t to a file, maybe rule1_time, so that the two calls to timer don't require a common variable. Oh, and don't try to use call inside a command:

STIME = date '+%s' > $@_time
ETIME = read st < $@_time ; echo $((`date '+%s'`-$st))

all:
    $(STIME)
    do stuff
    $(ETIME)

EDIT:
I wrote the code above as a proof of concept; I was going for clarity, not refinement. If I understand your comment correctly, you now want to know how to break down the time into hours, minutes and seconds, without calling several functions from each rule. There are several ways to do it, this is probably the cleanest:

ETIME = @read st < $@_time ; st=$((`date '+%s'`-$st-68400)) ; echo Elapsed time: `date -d @$st '+%H:%M:%S'`
阳光的暖冬 2024-11-12 15:44:24

如果您试图获取构建中步骤的计时信息,更好的解决方案是使用更智能的 make。 Electric Cloud 中的 ElectricMake 可以生成构建日志的 XML 标记版本,称为注释文件,其中包括构建调用的每个命令的确切计时数据,以及大量其他信息,例如使用的确切命令行(即使您已经使用了>@ 前缀)以及每个命令使用的环境变量。

您可以使用 ElectricMake 的免费版本 SparkBuild 免费试用。

If you're trying to get timing information for the steps in your build, a better solution is to use a smarter make. ElectricMake from Electric Cloud can generate an XML-marked-up version of the build log, called an annotation file, which includes the exact timing data for every command invoked by the build, as well a boatload of other information like the exact command-lines used (even if you have used the @ prefix) and the environment variables used by each command.

You can try it out for free with SparkBuild, a free version of ElectricMake.

遇见了你 2024-11-12 15:44:24

您可以使用 time:

all:
    @@time( \
        rsync -az ${src1} ${dest1} \
        && rsync -az ${src2} ${dest2} \
        ## etc.
    )

不能在此处使用制表符,但请记住仅在 makefile 中使用制表符(正如 POSIX 要求的那样)。

you can use time:

all:
    @@time( \
        rsync -az ${src1} ${dest1} \
        && rsync -az ${src2} ${dest2} \
        ## etc.
    )

can't use tabs in here, but remember to use only tabs in makefiles (as POSIX requires that).

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