|
|
small improvements on improvements
You forgot:-
4. WAY more efficient.
With the perl versions, when you run the script, your shell has to
fork and exec perl . Perl compiles the script then when it gets to
the system() command it forks and execs a shell, that shell goes and
finds the "open" program, forks yet again and execs it.
In the shell version, everything but "touch" and open" are shell
builtins, so much less forking going on, no expensive perl
interpreter startup.
For bonus points, make the last line "exec open ......" which saves
one fork operation.
For double bonus points, turn it into a shell function and load that
from your .bashrc (assuming you use bash as your login shell), something
like this:-
pb () {
for i in "$@"
do
test -e "$i" || touch "$i"
done
open -a "/Developer/Applications/Project Builder.app" "$@"
# NB you *don't* want that extra exec here or you'll blow
# away your login shell :)
}
(Oh yeah, you forgot the quotes around the $i in your version, so filenames
with spaces will leave "droppings" all over the place)
Anyway, with that shell function defined, you can type "pb foo.c"
as before and if the file exists, the only time the shell will have
to fork and exec is when it runs the "open" command, which saves about
a bazillion CPU cycles and disk IOs compared to the perl way.
Obviously in csh people's mileage will vary, but scripting in csh
is evil, so don't do that. For other Bourne shell variants, the startup file to place shell functions into is different.
-- Pete.
small improvements on improvements
Good point about the quotes...that one slipped by me. Your efficiency suggestions are all good ones, too. I will only note that you have that peculiar obsession with micro-optimization that characterizes many old-time Unix people, which is, I suppose, understandable seeing as how the old Unix machines probably would get smoked by my PalmPilot :-).
small improvements on improvements
More of a habit than an obsession... I just hate seeing cycles go to waste.
small improvements on improvements
> WAY more efficient.
small improvements on improvements
well, making it a shell function avoids
small improvements on improvements
Certainly making it a shell function rather than a separate script
heh
I'm amused that you actually went and timed this. You are, of course, absolutely right that in the big scheme of things, the overhead of running perl vs. using a shell function is very small potatoes. I think of this sort of optimization as more of a fun game than anything else (you can feel free to call me sick now).
small improvements on improvements-BUG
Um, neat trick! |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.15 seconds |
|