Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elapsed time for each rule

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

like image 466
jotapdiez Avatar asked May 09 '11 16:05

jotapdiez


People also ask

How do you calculate elapsed time?

In simple words, we can say that the amount of time that has passed between the beginning and the end of an event is called the elapsed time. We can determine this time by subtracting the end time and the start time. The formula to calculate the elapsed time is simply to subtract the hours and minutes separately.

What is example of elapsed time?

Definition of Elapsed Time Elapsed time is the amount of time that passes between the beginning and the end of an event. For example, Vernon ran a marathon in 2 hours and 15 minutes. He crossed the finish line at 10:30 a.m. What time did the race start?

What's elapsed time?

: the actual time taken (as by a boat or automobile in traveling over a racecourse)

How do you use elapsed time in a sentence?

No association was found between grief reactions and the elapsed time since the child's death. Thus, based on elapsed time, we would incorrectly conclude that the two behaviors are different. The elapsed time for the 2.54 cm of deionized water (non-constant head) to infiltrate the soil surface was recorded.


1 Answers

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'`
like image 183
Beta Avatar answered Sep 19 '22 19:09

Beta