News for hackers. Stuff that matters.
Re:/usr/bin/time: not the command you think you know by jimrandomh on Monday March 06, 2017 @10:29PM (#13808054)
The reason 'time' is a builtin is because it can time a complete shell pipeline, not just a single command. If you type

    time foo |bar
Then the result is the total time taken by foo and bar together. This requires it to have special-case syntax. Whereas

    /usr/bin/time foo |bar
Would run foo and give its time statistics as input to bar.
Re:/usr/bin/time: not the command you think you know by jcoffland on Tuesday March 07, 2017 @02:16AM (#13808814)
Run these commands for a better demonstration:

    time echo | sleep 1
vs.

    /usr/bin/time echo | sleep 1
The former times the entire pipeline whereas the later only times the first command in the pipe.
Re:/usr/bin/time: not the command you think you know by fnord123 on Tuesday March 07, 2017 @05:09AM (#13809386)
This also gives the time of the entire pipeline (since the commands all begin together and only finish when the last command has completed):

echo | /usr/bin/time sleep 1

Re:/usr/bin/time: not the command you think you know by roblabla on Tuesday March 07, 2017 @07:31AM (#13809850)
I'm pretty sure this is wrong.

As an example :

sleep 2 | /usr/bin/time sleep 1

This gives an output of "1 second" even though the entire pipeline took 2 seconds.

Re:/usr/bin/time: not the command you think you know by fnord123 on Tuesday March 07, 2017 @08:15AM (#13810037)
Ok you can wangle your way out of it. But if you're doing something like `grep "$something" | /usr/bin/time some_job` then generally you're fine.
Re:/usr/bin/time: not the command you think you know by perennate on Tuesday March 07, 2017 @08:46AM (#13810227)
Wouldn't the system/user time be wrong though?
Re:/usr/bin/time: not the command you think you know by exprA on Tuesday March 07, 2017 @08:06AM (#13809997)
The sleep sub-process of time is not part of the pipeline, however. It will exit after the requested time has passed.
Re:/usr/bin/time: not the command you think you know by ramshorns on Monday March 06, 2017 @11:29PM (#13808292)
It looks like

  /usr/bin/time foo |bar
would actually give the output of foo as input to bar, since the time statistics go to stderr.
Re:/usr/bin/time: not the command you think you know by dotancohen on Tuesday March 07, 2017 @01:42AM (#13808704)
Since it is not an error, what we really need is a stdmeta file descriptor:

http://unix.stackexchange.com/questions/197809/propose-addit...

Re:/usr/bin/time: not the command you think you know by eriknstr on Tuesday March 07, 2017 @05:08AM (#13809383)
I've been thinking a bit about this before and I think that not just one but a whole lot of additional streams would be useful. By default all commands would still use stdout and stderr only but you would be able to tell them to use other streams as well for their various data.

Furthermore, each stream should be possible to pipe to a different process if you wish independently of the other streams.

To a small degree this might be possible already with named pipes.

Re:/usr/bin/time: not the command you think you know by ncw33 on Tuesday March 07, 2017 @09:03AM (#13810340)
It is possible already - you can do `command 2>&4` to separate the output from one command in a pipeline into its own stream, then do `command2 <&4` to pull from the further down the pipeline.
Re:/usr/bin/time: not the command you think you know by eriknstr on Tuesday March 07, 2017 @10:54PM (#13817752)
I would like more pipes for each process.

This would allow many things, for example searching for multiple terms across all files in the whole file system in one go without opening each file more than once, and then grouping the different results together in accordance with the term that they matched.

Re:/usr/bin/time: not the command you think you know by Klathmon on Tuesday March 07, 2017 @07:29AM (#13809839)
Honestly this is one area where PowerShell kind of nailed it.

I still don't use it enough, but when it comes to scripting PS's objects beat the hell out of these

Re:/usr/bin/time: not the command you think you know by MrQuincle on Tuesday March 07, 2017 @05:36AM (#13809476)
I just started playing around with IPC ideas with processes tellin each other which flags they would like: https://github.com/mrquincle/bipipes

I think socket pairs would be great for it.

Nothing implemented yet!

Re:/usr/bin/time: not the command you think you know by easytiger on Tuesday March 07, 2017 @03:42AM (#13809095)
Just think of it as std dbg, not err
Re:/usr/bin/time: not the command you think you know by Merovius on Tuesday March 07, 2017 @11:18AM (#13811569)
No, it wouldn't require special-case syntax. It could just pass it's argument to '/bin/sh -c'. You'd have to shell-escape the pipeline, though.
Re:/usr/bin/time: not the command you think you know by eridius on Tuesday March 07, 2017 @04:50PM (#13815325)
Yeah it does require special-case syntax. When using bash's built-in time, the command is executed exactly the same way as if you weren't using time, which means the command can set variables or define functions in the current shell. For example, you can say

  time foo=bar
  echo $foo
and it will correctly print out "bar". It can't do this if it executes the command in a subshell. Or similarly you can say `time cd /foo` and it will actually change the current working directory.
Re:/usr/bin/time: not the command you think you know by chubot on Monday March 06, 2017 @11:00PM (#13808165)
Yes, as a consequence, it also lets you do this:

    time {
      sleep 0.1
      sleep 0.2
    }
    # result is 0.3 seconds
 
which you obviously can't do with an external command. I wrote about that here:

https://lobste.rs/s/hwgt2h/usr_bin_time_not_command_you_thin...

Re:/usr/bin/time: not the command you think you know by arthur2e5 on Monday March 06, 2017 @11:35PM (#13808305)
The `time` syntax in combination with for loops or just shell functions is pretty useful as well.
Re:/usr/bin/time: not the command you think you know by jstimpfle on Tuesday March 07, 2017 @04:10AM (#13809189)
That is merely an optimization though. You can simply

    time sh -c 'foo | bar'
Re:/usr/bin/time: not the command you think you know by therein on Tuesday March 07, 2017 @04:35AM (#13809273)
But now you are timing the startup of `sh` as well.
Re:/usr/bin/time: not the command you think you know by aexaey on Tuesday March 07, 2017 @06:50AM (#13809711)
...which (surprisingly) is a measurable amount. About 1 ms for `dash` and a whooping 9.5 ms for `bash` on a relatively slow CPU:

  pi@pi:~ $ time (for x in {1..1000}; do time echo | sleep 0.01; done 2>/dev/null)
  real    0m24.173s

  pi@pi:~ $ time (for x in {1..1000}; do time sh -c 'echo | sleep 0.01'; done 2>/dev/null)
  real    0m25.170s

  pi@pi:~ $ time (for x in {1..1000}; do time bash -c 'echo | sleep 0.01'; done 2>/dev/null)
  real    0m33.643s
Re:/usr/bin/time: not the command you think you know by jstimpfle on Tuesday March 07, 2017 @11:50AM (#13811946)
1ms is on the order of process creation time, so not very relevant: The measured program itself requires process creation. Most programs run considerably longer than that (otherwise timing would not make much sense).

It's really not more than that: an optimization.

Re:/usr/bin/time: not the command you think you know by floatboth on Tuesday March 07, 2017 @12:02PM (#13812100)
Wow — that is extremely surprising. I did not expect that behavior at all. Who the hell thought it was a good idea to let a builtin command absorb the pipe symbol?!
Re:/usr/bin/time: not the command you think you know by inetknght on Tuesday March 07, 2017 @11:08AM (#13811482)
`/usr/bin/time -o foo_time.txt foo | bar`