why is ruby so damn slow?
I don't understand this. I rewrote a /bin/sh script in
ruby and the sh script runs much faster. I ran the script on
both Linux and Solaris. The script removes a symlink and
replaces it with a copy of the linked to file.
The ruby script took 20 seconds and the sh script took 7
seconds. I don't remember the file size. I don't understand
the difference. The sh script looks so sloppy.
here's the core of the sh script:
#!/bin/sh
islink=`ls -l $1 | grep '>'`
if [ ".${islink}" = "." ]
then
echo cpol: $1 is not a link
exit
fi
tempfrom=`ls -l $1 | cut -d'>' -f2`
xfilename="`basename $1`"
xdirname="`dirname $1`"
cd $xdirname
if [ -r $tempfrom ]
then
echo " cpol: replacing $xfilename in $xdirname with $tempfrom"
rm -f $xfilename
cp $tempfrom $xfilename
if [ $? != 0 ]
then
echo " cpol: return status is $? from cp"
echo " cpol: will try to re-establish the link even though it may be bad "
echo " - to be available future reference "
ln -s $tempfrom $xfilename
else chmod u+w $xfilename
fi
And here's the ruby script:
#!/usr/bin/ruby
#puts "#{ARGV.length}"
require 'ftools'
if ARGV.length == 0 || ARGV.length > 2
puts "Usage: cpol [ source ] destination."
exit
elsif ARGV.length == 1
dest = ARGV.shift
destDir = File.dirname (dest)
if (!FileTest.symlink?(dest))
puts "file #{dest} is not a link"
exit
end
src = File.expand_path(File.readlink(dest), destDir)
if (!FileTest.readable?(src)) # necessary, readlink succeeds even if not
puts "#{src} not readable"
exit
end
File.delete(dest)
File.syscopy(src,dest)
File.executable?(src) ? File.chmod(0755, dest) : File.chmod(0644, dest)
elsif ARGV.length == 2
src = ARGV.shift
dest = ARGV.shift
end