How can I get the CPU time for a perl system call?

后端 未结 3 1914
梦毁少年i
梦毁少年i 2021-01-22 20:00

I have a perl script that calls external executables using system(). I would like to measure the CPU seconds taken by these external programs. Ideally, I would like

3条回答
  •  青春惊慌失措
    2021-01-22 20:41

    $ time ( ls ) 2> er ## works
    $ perl -e 'system("time (ls)")'
    sh: 1: Syntax error: word unexpected (expecting ")")
    

    The problem is that in first case, your shell is probably /bin/bash whereas in second case it is /bin/sh. If you want to run your command with another shell, you could use system LIST form:

    system("/bin/bash", "-c", "time(ls)")
    

    Note 1: There's PERL5SHELL environmnet value, but that seems to take effect only on Win32.

    Note 2: If you want to measure CPU time of child process, you could use Unix::Getrusage or BSD::Resource modules.

提交回复
热议问题