5 ms·
I did a quick test, and function calls using the subshell type of function seems to be about twice as slow as normal function calls. #!/bin/bash f
by zegl 5y ago
I did a quick test, and function calls using the subshell type of function seems to be about twice as slow as normal function calls.
#!/bin/bash
fib1() {
if (( $1 < 2 )) ; then
echo $1
return 0
fi
echo $(( $(fib1 $(($1-1))) + $(fib1 $(($1-2))) ))
return 0
}
fib2() (
if (( $1 < 2 )) ; then
echo $1
return 0
fi
echo $(( $(fib2 $(($1-1))) + $(fib2 $(($1-2))) ))
return 0
)
time fib1 15
time fib2 15
Result:
610
real 0m1.585s
user 0m0.590s
sys 0m0.972s
610
real 0m3.168s
user 0m1.068s
sys 0m2.025s
- jrochkind1 5y agoIt probably doesn't matter too much if you have only a handful of function invocations in your exec. But if you have a a couple orders of magnitude more... RAM is going to be an issue too maybe. Creating a new process for every single function invocation seems crazy to me, and but might actually be just fine for many "ordinary" use cases? (Although might not have been on computers of 20+ years ago, which might also be why it's not something advised, so much of bash tradition is decades old?)
- kevincox 5y agoOf course the subshell is copy-on-write so the ram requirements shouldn't be huge. But assuming some stack you are using at least some stack in each process you are looking at 4k per call which adds up fairly quickly.