# time function

**URL:** <https://community.unix.com/t/time-function/169825>\
**Category:** Shell Programming and Scripting\
**Created:** [March 16, 2007, 5:44am UTC](https://community.unix.com/t/time-function/169825 "2007-03-16T05:44:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsekol](https://community.unix.com/letter_avatar/hsekol/32/5_5575768a8748004e209b776fc1b2916d.png) [@hsekol](https://community.unix.com/u/hsekol)\
**Post date:** [March 16, 2007, 5:44am UTC](https://community.unix.com/t/time-function/169825/1 "2007-03-16T05:44:58Z")

</div>

Hi,

```
   I would like to display the exact time taken to complete running a particular tool or function or program to user

   I don't know the exact time functions in unix, please help me

 thanks in advance

```

```nohighlight
Example:
     
    $test.ksh

    output should be
    The time taken to execute the test.ksh file is 00.30.00 seconds

```

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [March 16, 2007, 5:49am UTC](https://community.unix.com/t/time-function/169825/2 "2007-03-16T05:49:54Z")

</div>

```nohighlight
time test.ksh

```

---

<div class="post-metadata">

**Author:** ![hsekol](https://community.unix.com/letter_avatar/hsekol/32/5_5575768a8748004e209b776fc1b2916d.png) [@hsekol](https://community.unix.com/u/hsekol)\
**Post date:** [March 16, 2007, 5:58am UTC](https://community.unix.com/t/time-function/169825/3 "2007-03-16T05:58:03Z")

</div>

hi anbu,

```
    the code 

```

```nohighlight
 time test.ksh

```

```
    is working when i am executing my file from command line, but i want to include the code inside my code, how can i achieve that one.
```

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [March 16, 2007, 6:02am UTC](https://community.unix.com/t/time-function/169825/4 "2007-03-16T06:02:09Z")

</div>

create a string that forms the command as run in command line,

```nohighlight
char command[50];
sprintf(command, "time %s", commandtorun);

```

and you have to run this " command " with " popen "  
which would execute and you have would have execution time in the pipe, which you need to parse it and display! 🙂

Just a pointer !  
If you need more explanations, shoot ! 🙂

---

<div class="post-metadata">

**Author:** ![hsekol](https://community.unix.com/letter_avatar/hsekol/32/5_5575768a8748004e209b776fc1b2916d.png) [@hsekol](https://community.unix.com/u/hsekol)\
**Post date:** [March 19, 2007, 6:55am UTC](https://community.unix.com/t/time-function/169825/5 "2007-03-19T06:55:10Z")

</div>

hi matrixmadhan,

```
 thanks for your reply,

 the code

```

```nohighlight
     char command[50];
sprintf(command, "time %s", commandtorun);

```

```
I think code which u have given will not work in ksh program. can u send me the code relavent to ksh

```

thanks,  
hsekol

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [March 19, 2007, 6:56am UTC](https://community.unix.com/t/time-function/169825/6 "2007-03-19T06:56:55Z")

</div>

do you want to experiment that with the shell - command line or in a code ?

am not clear about that!

If that is in a code, use what I had specified  
if not, to be used in a shell, you can use

```nohighlight
time
times

```

Else,  
post your specific requirement! 🙂

---

<div class="post-metadata">

**Author:** ![hsekol](https://community.unix.com/letter_avatar/hsekol/32/5_5575768a8748004e209b776fc1b2916d.png) [@hsekol](https://community.unix.com/u/hsekol)\
**Post date:** [March 19, 2007, 7:16am UTC](https://community.unix.com/t/time-function/169825/7 "2007-03-19T07:16:01Z")

</div>

Hi matrixmadhan,

```
 thanks for your quick reply,

 my requirement is 

 Example:

```

```nohighlight
    echo "Enter i value"
      read i
      n=0
      while [$n -eq $i]
      do
          echo $i
          i=`expr $i + 1`
      done

```

```
 let's assume that for the first time, i have given 50 as i value, so while loop will execute for 50 times and atlast the program have to display the actual time taken to complete program to execute \(this one i want for performance\) this program i have written in ksh

I think u got my requirement, i have write the time function in side the code only
```

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [March 19, 2007, 7:32am UTC](https://community.unix.com/t/time-function/169825/8 "2007-03-19T07:32:15Z")

</div>

and this,  
🙂

```nohighlight
#! /bin/zsh

i=0

read n < /dev/tty

start=`date '+%s'`
while [$i -le $n]
do
  i=$(($i + 1))
done

echo "diff is $((`date '+%s'` - $start)) second(s)"

exit 0

```

---

<div class="post-metadata">

**Author:** ![hsekol](https://community.unix.com/letter_avatar/hsekol/32/5_5575768a8748004e209b776fc1b2916d.png) [@hsekol](https://community.unix.com/u/hsekol)\
**Post date:** [March 19, 2007, 8:19am UTC](https://community.unix.com/t/time-function/169825/9 "2007-03-19T08:19:44Z")

</div>

thanks madan, the code which u have given is working...thanxs a lot

---

<div class="post-metadata">

**Author:** ![ennstate](https://community.unix.com/user_avatar/community.unix.com/ennstate/32/514_2.png) [@ennstate](https://community.unix.com/u/ennstate)\
**Post date:** [March 19, 2007, 10:06am UTC](https://community.unix.com/t/time-function/169825/10 "2007-03-19T10:06:37Z")

</div>

$ cat /tmp/time.ksh  
#!/bin/ksh  
let i=0  
while [[i -le 1000]] ; do  
echo $i  
((i=i+1))  
done  
echo "This has taken $SECONDS to complete"

SECONDS holds the number seconds since the shell was invocated.But this may not provide microseconds resolutions.

Thanks  
Nagaraj G

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [March 20, 2007, 12:11am UTC](https://community.unix.com/t/time-function/169825/11 "2007-03-20T00:11:54Z")

</div>

I dont think you could block the amount of time taken to execute within blocks within a code,

as posted by the OP,

read operation might take a subtle amount of time to pass on the value for the for loop where the effective time taken is calculated.

🙂
