call vs pipe

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

call vs pipe

#1 Post by npocmaka_ » 19 Apr 2016 08:46

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: call vs pipe

#2 Post by jeb » 19 Apr 2016 17:02

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: call vs pipe

#3 Post by dbenham » 19 Apr 2016 23:37

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

Post Reply