To print execution time of Script from Script? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of To print execution time of Script from Script? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m searching PDF files in all directories except those in .texpadtmp by code originally based on this discussion.

function findpdfs() {
find . -iname '*.pdf' -not -path "*.texpadtmp*" | while read filename
  echo -e "33[34;1m// === PDF Document:33[33;1m $filename33[0m"
  pdftotext -enc Latin1 "$filename" - | grep --color=always -i $1
  time         # TODO problem here!

where, in the last line, I try to print the execution time of the script for each file unsuccessfully.

How can you print the time correctly?

My next goal is then to have all execution times and present them as percentages in the output as whole numbers so I can approximate where most of time resources are going.


Method #1 – Using begin/end time

You’ll typically see it done where you do:

begin=$(date +%s) 
... do something ...
end=$(date +%s)

And then subtract the $begin time from the $end time to get the number of seconds that have elapsed.

tottime=$(expr $end - $begin)

Method #2 – Using the time commands

The other method would be to use the time command that’s built into Bash or the time program that's included with most Linux/Unix systems here,/bin/time`.

You can check like so:

$ type -a time
time is a shell keyword
time is /usr/bin/time
time is /bin/time

You use it like so:

$ time sleep 5

real    0m5.004s
user    0m0.001s
sys 0m0.001s
standalone executable
$ /bin/time sleep 5
0.00user 0.00system 0:05.00elapsed 0%CPU (0avgtext+0avgdata 628maxresident)k
0inputs+0outputs (0major+191minor)pagefaults 0swaps

They provide different outputs but you can use which ever suites your needs. Both will print the elapse time that a command takes to run:

$ time <cmd>

So you could prefix your pdf2text command.

We are here to answer your question about To print execution time of Script from Script? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji