1

Is there a command that's useful for doing a benchmark test on a separate command or function; to see how cpu intensive it is?

Preferably something similar to how you can report the duration of a command with time.

For instance: I tried the following code; before adding sleep 0.1; and it stressed the CPU, fans kicked into high gear, etc. Adding sleep 0.1 after each printf seemed to mitigate the problem. And basically, I'm just curious to check out the difference:

spin ()
{
    i=0 
    sp='/-\|' 
    n=${#sp} 
    printf ' ' 
    sleep 0.1
    while true; do 
        printf '\b%s' "${sp:i++%n:1}" 
        sleep 0.1
    done
}

Source: http://mywiki.wookedge.org/BashFAQ/034

voices
  • 1,272
  • 1
    sounds like you're looking for profiling tools. see for example http://stackoverflow.com/a/2229388 (perf and friends) – quixotic Mar 31 '17 at 06:31
  • Try /usr/bin/time -v ./yourscript.sh to get many informations about resource usage. – ctx Mar 31 '17 at 11:31

2 Answers2

0

time gives you the information! It tells you how much time the program spent as user (i.e. CPU usage) vs system (i.e. input/output). A CPU intensive program is one for which user time dominates and system time is negligible.

0

Seems there is a bash builtin/keyword time and a separate application, /usr/bin/time. Looks like the former takes precedence, so the full path is required for the latter.

(Apparently this has been covered a lot in the past, but it's the first time I've encountered it).

Basic functionality is the same, but the latter can provide additional information regarding CPU usage, and more (especially with the -v/--verbose flag).

While the former will accept a function as an operand, the latter will not; unless it's parsed in the context of a script. So, it's kind of annoying, and a minor inconvenience, but the only solution appears to be creating a new script; declaring a new function & calling it, saving it, then:

/usr/bin/time -v ./newfunction.sh

Thanks, everyone who took the time to write in.

voices
  • 1,272