Page 1 of 1

call vs pipe

Posted: 19 Apr 2016 08:46
by npocmaka_
I wonder if anybody else wondered if there's a point to use pipe instead of call (and eventually subroutine) as pipe can also be used for doubled expansion like call and can shortens some expression (e.g. if and for cannot be called but can be used with pipe).Unfortunately seems that pipe is around 6 times slower than calling subroutine (when the subroutine is close to the call).Probably because it creates new cmd processes on both sides of the pipe :(.

So I suppose it's better to use subroutines than pipes (pretty simple test):

Code: Select all

@echo off

set a=1
echo %time%
(
    for /l %%# in (1,1,1000) do (
        set a=2
        (break|echo %%a%%)>nul
    )
)
echo %time%
(
    for /l %%# in (1,1,1000) do (
        set a=3
        (call ::subr %%a%%)>nul
    )
)
echo %time%
exit /b %errorlevel%
:subr
echo %1


results on my machine:

Code: Select all

17:36:43.63
17:36:50.35
17:36:51.32

Re: call vs pipe

Posted: 19 Apr 2016 17:02
by jeb
Hi npocmaka,

pipes are interesting but nasty.

To use them for double expanding you would also need a FOR /F to fetch the result and then you creates three sub cmd.exe processes.
And the escaping rules can be a bit confusing.
The effects of code blocks with pipes are not obvious.
And it depends of the registry, if delayed expansion is enabled or not in the pipes.

And when you need a FOR /F loop, then you don't need to double expand with a pipe, you could use the FOR variable directly.

I would avoid pipes for such a simple task like double expansion, but for having fun with the side effects :D

Re: call vs pipe

Posted: 19 Apr 2016 23:37
by dbenham
There is no need for a pipe to get the effect. Simply use CMD /D /C directly

Code: Select all

cmd /d /c echo %%a%%

The only advantage over CALL that I can think of is it avoids quoted caret doubling.

Dave Benham