muの提案が短すぎるため、__ builtin_popcountを使用する単純なC拡張機能を作成し、ベンチマークを使用して、rubyの最適化された文字列関数よりも少なくとも3倍高速であることを確認しました。
次の2つのチュートリアルを見ました。
私のプログラムでは:
require './FastPopcount/fastpopcount.so'
include FastPopcount
def hamming(a,b)
popcount(a^b)
end
次に、プログラムを含むディレクトリに、次のファイルを含むフォルダ「PopCount」を作成します。
extconf.rb:
# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'
# Give it a name
extension_name = 'fastpopcount'
# The destination
dir_config(extension_name)
# Do the work
create_makefile(extension_name)
popcount.c:
// Include the Ruby headers and goodies
#include "ruby.h"
// Defining a space for information and references about the module to be stored internally
VALUE FastPopcount = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_fastpopcount();
// Prototype for our method 'popcount' - methods are prefixed by 'method_' here
VALUE method_popcount(int argc, VALUE *argv, VALUE self);
// The initialization method for this module
void Init_fastpopcount() {
FastPopcount = rb_define_module("FastPopcount");
rb_define_method(FastPopcount, "popcount", method_popcount, 1);
}
// Our 'popcount' method.. it uses the builtin popcount
VALUE method_popcount(int argc, VALUE *argv, VALUE self) {
return INT2NUM(__builtin_popcount(NUM2UINT(argv)));
}
次に、popcountディレクトリで次のコマンドを実行します。
ruby extconf.rb make
次に、プログラムを実行すると、ルビーでハミング距離を実行する最速の方法が得られます。