Tuesday, September 28, 2010

Compile ruby 1.9.2 on Ubuntu

assumes you have build-essential package installed
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p0.tar.bz2
tar -xjvf ruby-1.9.2-p0.tar.bz2
cd ruby-1.9.2-p0
./configure --enable-shared --program-suffix=1.9.2
make check # optional
sudo checkinstall make install
# [follow the defaults until here:
# NOTE that you want the extra files inside your directory.
# those extra files contain things like the openssl libraries!!

    Some of the files created by the installation are inside the build
    directory: /home/jtprince/src/ruby-1.9.2-p0

    You probably don't want them to be included in the package,
    especially if they are inside your home directory.
    Do you want me to list them?  [n]: n
    Should I exclude them from the package? (Saying yes is a good idea)  [y]: n

# At this point, you can install the package on machines of the same
# architecture with "sudo dpkg -i ruby-1.9.2_p0-1_amd64.deb" [depending on your arch]

sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby1.9.2 /usr/bin/ruby

Okay, now you need to get rubygems working properly. Remember, rubygems comes with ruby 1.9.X, so you don't need to download it separately.

At this point, we have to ensure our soft links point where we want and to tell gems to point to ruby 1.9.2

sudo cp /usr/bin/gem1.9.{1,2}
# then edit /usr/bin/gem1.9.2
# change #!/usr/bin/ruby1.9.1 ==> #!/usr/local/bin/ruby1.9.2

# now set up the soft link so that "gem" calls our gem1.9.2
sudo rm /etc/alternatives/gem
sudo ln -s /usr/bin/gem1.9.2 /etc/alternatives/gem

Maybe it is unnecessary, but I moved my local gem folder so that all my gems are compiled from scratch again to ensure they have been built against 1.9.2. how to set up a local gem environment

mv ~/.gem/ruby/{,not_using_}1.9.1
mkdir ~/.gem/ruby/1.9.2

Tuesday, September 21, 2010

Installing graphviz on Ubuntu Lucid Lynx 10.04

If you want sfdp (a great layout for large graphs), then you need the .debs from the graphviz site (not through the ubuntu repos).

Download the develpment snapshots here
note, if you don't use the language specific bindings listed, you don't need to download those.

sudo aptitude install libdevil1c2 libgtkglext1
sudo dpkg -i graphviz_*.deb graphviz-dev*_all.deb 
graphviz-doc*_all.deb libgraphviz4_*.deb libgraphviz-dev_*.deb libgv-ruby_*.deb

Now, you can run sfdp!

Sunday, September 5, 2010

for anything complicated, ruby eats bash for breakfast

http://www.sklav.com/node/4 shows a bash solution to the issue debated here. Regardless of how good a bash script is, it inevitably is not very DRY, I think because it is hard to be DRY in bash scripting.

Here is a ruby solution. Of course, all I did was factor, which is easy in ruby:

#!/usr/bin/env ruby

lame_opts = "--preset standard -q0"

if ARGV.size == 0
  puts "usage: #{File.basename(__FILE__)} .flac ..."
  puts "output: .mp3"
  puts ""
  puts "uses lame opts: #{lame_opts}"
  puts "retains tag info"

tag_convert = {
  :tt => "TITLE",
  :tl => "ALBUM",
  :ta => "ARTIST",
  :tn => "TRACKNUMBER",
  :tg => "GENRE",
  :ty => "DATE",

ARGV.each do |file|
  tag_opts = tag_convert.map do |key,val|
    # comes out as TITLE=
    data = `metaflac --show-tag=#{val} #{file}`.split("=",2).last
    "--#{key} #{data}"
  mp3name = file.chomp(File.extname(file)) + ".mp3"
  `flac -dc #{file} | lame #{lame_opts} #{tag_opts.join(" ")} --add-id3v2 - #{mp3name}`
Fairly easy to follow, especially since we don't repeat ourselves. Most of the code is in creating a useful help message.

For Perl hackers, here is the entire script on a few lines of less than 105 chars per line:

tag_convert = {:tt=>"TITLE",:tl=>"ALBUM",:ta=>"ARTIST",:tn=>"TRACKNUMBER",:tg=>"GENRE",:ty=>"DATE"}
ARGV.each do |f| 
  tgs = tag_convert.map {|k,v| "--#{k} "+`metaflac --show-tag=#{v} #{f}`.split("=",2).last}.join(" ")
  `flac -dc #{f} | lame --preset standard -q0 #{tgs} --add-id3v2 - #{f.sub(/.flac$/i,".mp3")}`
There are still ways to compress this further, but it is very concise and surprisingly easy to follow (if you know ruby).

Wednesday, September 1, 2010

For sets of integers, lookup speed goes: Array > Hash > Set

You have some set of integers and you need to query it repeatedly to determine if a member is in it or not.

require 'benchmark'
require 'set'

ar = (0...1000).to_a

ar_index = []
hash_index = {}
ar.each do |v|
  ar_index[v] = true
  hash_index[v] = true

set_index = Set.new(ar)
access = (0...1000).to_a

TIMES = 10000

Benchmark.bmbm do |r|
  r.report("array index") { TIMES.times { access.each {|v| ar_index[v] } } }
  r.report("hash index") { TIMES.times { access.each {|v| hash_index[v] } } }
  r.report("hash include") { TIMES.times { access.each {|v| hash_index.include?(v) } } }
  r.report("set include") { TIMES.times { access.each {|v| set_index.include?(v) } } }
The output (ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]):

                   user     system      total        real
array index    0.730000   0.000000   0.730000 (  0.745341)
hash index     0.990000   0.000000   0.990000 (  1.003517)
hash include   1.310000   0.000000   1.310000 (  1.332846)
set include    1.970000   0.000000   1.970000 (  2.024808)

It's faster to numerically round than to sprintf in ruby

require 'benchmark'

class Float
  def round_to(x)
    (self * 10**x).round.to_f / 10**x

  def ceil_to(x)
    (self * 10**x).ceil.to_f / 10**x

  def floor_to(x)
    (self * 10**x).floor.to_f / 10**x

  def round_to_sprintf_f(x)
    ("%.#{x}f" % x).to_f

  def round_to_sprintf_sprintf(x)
    sprintf("%.#{x}f", x)

  def round_to_sprintf_modulus(x)
    "%.#{x}f" % x


num = 34.2342134
place = 3

TIMES = 1000000
Benchmark.bmbm do |r|
  r.report("numeric round_to (float)") { TIMES.times { num.round_to(place) } }
  r.report("numeric round_to (to string)") { TIMES.times { num.round_to(place).to_s } }
  r.report("sprintf (float)") { TIMES.times { num.round_to_sprintf_f(place) } }
  r.report("sprintf (string)") { TIMES.times { num.round_to_sprintf_sprintf(place) } }
  r.report("sprintf modulus (string)") { TIMES.times { num.round_to_sprintf_modulus(place) } }
The output:

% ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
                                   user     system      total        real
numeric round_to (float)       0.820000   0.000000   0.820000 (  0.819920)
numeric round_to (to string)   1.990000   0.000000   1.990000 (  1.989912)
sprintf (float)                3.620000   0.000000   3.620000 (  3.614758)
sprintf (string)               2.650000   0.000000   2.650000 (  2.654812)
sprintf modulus (string)       3.190000   0.000000   3.190000 (  3.191807)

The conclusion: Regardless of whether you are going to a string or numeric after rounding, it is faster to round numerically than with sprintf. Also, using the sprintf function is faster than '%'. Usually speed doesn't matter, but when it does...