TL;DR:: bash/zsh parameter expansion is faster than cut.

which is faster?

Consider you want to take out hostname from URL or IP with port like some.mysql.server:3306 or 192.168.1.10:3306 using bash/zsh. There are some way to do this.

The first way is using cut command:

1
2
$ TARGET="192.168.1.10:3306"
$ echo ${TARGET} | cut -d ":" -f 1

Now printed 192.168.1.10 on your screen.

The second way is using “shell parameter expansion”, which is functions of bash/zsh built-in. You can use shell parameter expansion like below:

1
2
$ TARGET="192.168.1.10:3306"
$ echo ${TARGET%:*}

Printed 192.168.1.10 too.

Which is faster, using cut or shell parameter expansion? I make a small benchmark.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ time for i in {1..1000}; do echo $(echo ${TARGET} | cut -d ':' -f 1); done > /dev/null

real	0m17.422s
user	0m1.325s
sys	0m1.890s
$ time for i in {1..1000}; do echo ${TARGET%:*}; done > /dev/null

real	0m0.008s
user	0m0.007s
sys	0m0.001s

Wow! In this situation, shell parameter expansion is faster than cut command solution!

Because this is very simple situation, shell parameter expantion is not always better than cut command. However, situations like this example, we should use shell parameter expansion.