in reply to Re^2: exec vs. backtick-and-assign performance
in thread exec vs. backtick-and-assign performance

How long does it take to run the command manually? It might just take very long to finish.

backticks will wait for the program to end before returning the output, so if the program just takes a long time to stop, you won't see any output since you're capturing it with the backticks. exec will just replace the current program with the new one, so any output of the command will be printed immediately, even if the program is still running.

by the way, if you're not doing anything with the output except printing you could also use system(), which doesn't capture the program's output, so any output will also be printed immediately (while the program is running, before system() returns).

About backticks using exec() - exec() is the only way to start a new program in unix. system(), backtics, piped open etc all use exec() behind the scenes. Calls that execute a command and return do a fork first, then call exec() in the child process. That's why exec() should always be the fastest. Not that you'd normally notice the speed difference - fork() is pretty fast (it has to be - it's the only way to start a new process).

  • Comment on Re^3: exec vs. backtick-and-assign performance

Replies are listed 'Best First'.
Re^4: exec vs. backtick-and-assign performance
by apotheon (Deacon) on Nov 12, 2006 at 23:48 UTC

    Running the program manually takes effectively zero seconds, as indicated by my mentions of the fact that running it with exec() is essentially instantaneous.

    Your reference to exec() being the "only way to start a new program" makes me think you're talking about the system call exec(), as opposed to the Perl function (which calls the unix system call). Is that what was meant in the previous post, then? I thought it was referring to the Perl function.

    print substr("Just another Perl hacker", 0, -2);
    - apotheon
    CopyWrite Chad Perrin

      Well perl's exec() function as far as I know does nothing more than call the sytem's exec(). update: but yes, I meant the underlying mechanics, not perl's buildin functions.

      Running the program manually takes effectively zero seconds, as indicated by my mentions of the fact that running it with exec() is essentially instantaneous.

      I thought you meant it started immediately, I didn't see anything about it also finishing quickly.

      It may be that the command is checking slowed down / confused by the capturing of it's STDOUT. it might be looking for a tty or something similar. In that case, the easiest solution should be to use system() (or exec(), ofcourse)

        "It may be that the command is checking slowed down / confused by the capturing of it's STDOUT. it might be looking for a tty or something similar."
        I guess that's the best idea I've heard so far for why it might behave like that. Anyone here know if restarting networking in Debian involves some kind of tty checking?

        print substr("Just another Perl hacker", 0, -2);
        - apotheon
        CopyWrite Chad Perrin