There must be something inside that script making it slow. Calling a minimal script on my 450 MHz Pentium-II with FreeBSD-4.x one tousand times takes less than two seconds.
In general, you can speed things up by not calling, but sourcing in new script. But watch for side-effects, variable settings are effective in the parent script and exit() will exit the outer shell.
That means you write
. foobar/newscript.sh
instead of
sh foobar/newscrip.sh
That is called "sourcing" the new script instead of "executing". Doing so is making it a factor of 16 faster for me.
But obviously the target script needs to be written with this in mind because of the side effects.
Now, here's the real MyCoy. You can shield yourself from the side effects by executing a subshell first:
(. foobar/newscript.sh)
That is much slower than just sourcing, but it is still twice as fast as just calling the script. The reason is that you have to fork() which is expensive but at least you don't exec(). Since the minimal script is almost empty this cuts the systemcalls in half.
Again, here's the timings:
executing: 500 calls/second
sourcing: 8000 calls/second
sourcing in subshell: 1000 calls/second
|