Finding processes using ALSA sound fast
You start a lot of processes here. Instead you can try doing in a similar way to the lsof script you posted... but replacing lsof by a shell for loop:
If you want to avoid launching lots of grep processes, start only one:
#!/bin/shfor i in /proc/[0-9]*/fd/*do echo ${i%/fd/*} $(readlink $i)done | grep -q /dev/snd/pcm
This takes now 4.5s on my desktop, compared to 7.5s when there's one grep process for each opened file.
But... your grep is not necessary here, I think. If you care so much, you can try:
#!/bin/shfor i in /proc/[0-9]*/fd/*do var="$(readlink $i)" if test x"$var" != x"${var#/dev/snd/pcm}" then echo $i fidone
This is even faster for me (test
is almost always a shell builtin), but I guess this is more because of bad testing methods. Try yourself.