0

i have the following problem that i would need your assistance to solve. I have a function lets call it "fun", and i want to time how much time the function needs to be run, so i have this line added in the script:

time fun;

So like this, it will print also the time that the function needed to run. Point is that i want to redirect the output of the time into a variable, so i can manipulate it later as i see fit. My failed tries already:

TIME=`time fun`
time fun | tail -3
etc..

Does anyone know how this can be done, so for example the desired result would be:

somehow add the time output in TIME variable.

so if i would echo $TIME I would get result like this,

real    0m0.45s
user    0m0.35s
sys     0m0.00s

Thank you for your time!!

P.S.

I run the script on ksh, on an oracle solaris system.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Sir. Hedgehog
  • 1,260
  • 3
  • 17
  • 40
  • 1
    Possible duplicate of [How do I redirect output to a variable in this shell function?](http://stackoverflow.com/questions/2559076/how-do-i-redirect-output-to-a-variable-in-this-shell-function) – Ani Menon Apr 28 '16 at 10:57
  • @AniMenon how is that dublicate with the one you are talking about.... i am asking about `time` function and to redirect to a variable... jesus.... also that is in bash, and i am asking about ksh.... – Sir. Hedgehog Apr 28 '16 at 11:01
  • the time function is working for you, your problem was in redirecting the output which is actually a duplicate of the above question. – Ani Menon Apr 28 '16 at 11:10
  • @AniMenon no, this is not a duplicate. `time` behaves differently, so the solution provided in the question you link is not useful here. Test it and test my answer to cross check. – fedorqui Apr 28 '16 at 11:13
  • My dear @AniMenon you still dont see it do you?? What was given as an answer there i already tried it, you could see it on my post, but also i see that you didnt bother reading it. If i would like to get the output of the function that would have worked fine for me, though i need somehow to catch what the time was returning, and that couldnt be done in that way, look at the solution fedorqui gave me, which is actually what i was looking for as well, and compare it with the selected answer from the post you marked as dublicate. Not the same thing darling :) – Sir. Hedgehog Apr 28 '16 at 11:16
  • hedgehog cool. And @fedorqui yes the redirection is there here. – Ani Menon Apr 28 '16 at 13:47

2 Answers2

1

You can use what How can I redirect the output of 'time' to a variable or file? describes:

your_time=$( { time fun ; } 2>&1 >/dev/null)

Here we are redirecting stdout to /dev/null so that we get rid of it and just get stderr. This is a particular case of:

foo=$( { time ls; } 2>&1 )            # More efficient version.

Test

We are going to ls two files: one that exists (myfile) and one that does not (asdfjasdkfl).

$ touch myfile
$ time_info=$( { time ls myfile asdfjasdkfl; } 2>&1 >/dev/null)
$ echo "$time_info"
ls: cannot access asdfjasdkfl: No such file or directory

real    0m0.003s
user    0m0.000s
sys 0m0.000s
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    well i cannot open the link you sent me, cause my pc has a whitelist for broswer. but i just tried what you recommented and it works just fine, as always you have been targeted efficient and correct!! tnx fedorqui! – Sir. Hedgehog Apr 28 '16 at 11:08
  • 1
    I know what you meant on the edit you did on my post btw, but i just added it, because many times people dont bother to read what you have tagged and give you answers for other languages, like ksh - bash, thats why i added it on the post :) tnx anyways – Sir. Hedgehog Apr 28 '16 at 11:19
  • i got one more question for you regarding this matter.... i did as you told me and everything is working fine, so this is how it looks now. `TIME1=$( { time awkfun ; } 2>&1 >/dev/null);` .... but one problem now is that awkfun is actually printing some stuff, but now if i use it like this, it is running and i take the time, but it doesnt print the stuff.... Any suggestions??? – Sir. Hedgehog May 02 '16 at 09:05
  • @hedgehog you may want to check [`tee`](http://stackoverflow.com/q/1221833/1983854). – fedorqui May 02 '16 at 09:10
  • i see, that would be done though in the case that i needed to store the output, what i need it to do is just print it on the terminal. because the moment i used the /dev/null to recurse the output of time the actual functionality of the function awkfun which is to output something is gone. I really dont get why it doesnt print it as long as the whole script is running.... any ideas still? – Sir. Hedgehog May 03 '16 at 06:12
  • 1
    @hedgehog this sounds like you should ask a new question with all the details in it, since it is getting quite far away from this question! – fedorqui May 03 '16 at 06:22
0

Command:

variable=$(time fun)

Example :

var=$(time ls -l) ## or var="$(time ( ls -l ) 2>&1 >/dev/null )"
echo "$var"

Output:

##displays output of ls command and the time taken
real    0m0.003s
user    0m0.001s
sys     0m0.001s

Alternate method :

Just use time directly on your execution statement of the script.

Example : time ./fun

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • hmmmm, first of all the example you gave doesnt work i already tried it, you could see it on my post, second of all i am talking about a function in a script i am not talking about a whole script, there are much more things inside the whole script, so i cannot use time like that, it has to be used on a function. – Sir. Hedgehog Apr 28 '16 at 11:03
  • The example I have put up was tried inside a script & it works. You may also run it like this : `mytime="$(time ( ls -l ) 2>&1 >/dev/null )"` and then print it `echo $mytime` – Ani Menon Apr 28 '16 at 11:14
  • i didnt say your script doesnt work, i said that this it doesnt work for me. because it doesnt do what i need because time has a different behaviour with functions. anyways fedorqui answered it already before you, thanks for the reply though. – Sir. Hedgehog Apr 28 '16 at 11:38