Anwar
Anwar

Reputation: 1805

Will calling system command be faster than ruby method?

I am writing a ruby script to compare large number of debian packages' version. I have two options here, using debian systems' dpkg --compare-versions from ruby script or implement the comparison routine in Ruby.

My question is which will be faster? Should I rely on dpkg --compare-versions or implement the algorithm?

Note: I am assuming that the script will only be run from debian systems, though ability of running from non-debian system would be an extra benefit of course.

The version comparison algorithm is described here in Ubuntu policy

Upvotes: 1

Views: 116

Answers (2)

Frank Schmitt
Frank Schmitt

Reputation: 30765

Ruby-Debian might also be an alternative. According to the description:

ruby interface for dpkg

This package provides Debian::Dpkg and Debian::DpkgDeb modules and Debian::Deb, Debian::Dsc, Debian::Archives, Debian::Sources, Debian::Packages and Debian::Status classes for ruby

(unfortunately, the web page doesn't contain examples on how to use this, so I guess your best bet is to install it and play with it in an irb/pry session).

UPDATE

Here's a complete example, as per the instructions by @Amadan (I had to use require 'debian' instead of require 'ruby-debian')

Installation

apt-get install libapt-pkg-dev
gem install ruby-debian

Usage

require 'debian' 

puts Debian::Version.cmp_version("1.2.3", "<", "1.2.11")

Upvotes: 3

tbrisker
tbrisker

Reputation: 15666

Considering that dpkg is written mostly in C, and that quite a bit of effort went into making it as fast as possible, I believe it would be best to call the command rather then try to implement the comparison yourself. Even if you manage to make the algorithm as efficient as the one dpkg uses, you would still have the speed gap between C and Ruby you won't be able to overcome.

However, there is some overhead to creating multiple processes. Since you want to compare thousands of versions, you might want to figure out a data structure and/or algorithm that allows you to make multiple comparisons at once (or reduce the number of comparisons needed). The viability of this depends on your use case.

I would first try benchmarking with dpkg (as this doesn't require much work) and see if the runtime for it is fast enough for your requirements, before considering putting work into trying to optimize this.

Upvotes: 2

Related Questions