質問
Rubyでは、何が最も効率的な計算のビットの差分符号なし整数(例えばのハミング距離)?
例えば、整数a=2323409845b=1782647144.
そのバイナリ表現は:
a = 10001010011111000110101110110101
b = 01101010010000010000100101101000
このビットの差のa&bは17..
ができるかを論理XORし、そのよって異なる整数!= 17、そしてを繰り返し処理し、バイナリ表現の結果の集計の数1s.
その中で最も効率的な計算のビットの違いは?
現在の回答の変更の計算のビット差の配列の多くのint?E.g.た2つの配列が符号なし整数
x = {2323409845,641760420,509499086....}
y = {uint,uint,uint...}
その中で最も効率的な計算のビットの差の二つの配列か?
だの反復処理の配列から、より速く計算差額の全体でのシーケンスは一回しか出来ないのですか?
解決
利用できる最適化された文字列の機能をRubyのビット数ではなく、純粋な演算.すで約6倍速くにあるアットホームなベンチマーキング.
def h2(a, b)
(a^b).to_s(2).count("1")
end
h1は通常の計算をh2に変換するxorには、文字列数をカウントし、"1"s
ベンチマーク:
ruby-1.9.2-p180:001:0>> def h1(a, b)
ruby-1.9.2-p180:002:1*> ret = 0
ruby-1.9.2-p180:003:1*> xor = a ^ b
ruby-1.9.2-p180:004:1*> until xor == 0
ruby-1.9.2-p180:005:2*> ret += 1
ruby-1.9.2-p180:006:2*> xor &= xor - 1
ruby-1.9.2-p180:007:2*> end
ruby-1.9.2-p180:008:1*> ret
ruby-1.9.2-p180:009:1*> end
# => nil
ruby-1.9.2-p180:010:0>> def h2(a, b)
ruby-1.9.2-p180:011:1*> (a^b).to_s(2).count("1")
ruby-1.9.2-p180:012:1*> end
# => nil
ruby-1.9.2-p180:013:0>> h1(2323409845, 1782647144)
# => 17
ruby-1.9.2-p180:014:0>> h2(2323409845, 1782647144)
# => 17
ruby-1.9.2-p180:015:0>> quickbench(10**5) { h1(2323409845, 1782647144) }
Rehearsal ------------------------------------
2.060000 0.000000 2.060000 ( 1.944690)
--------------------------- total: 2.060000sec
user system total real
1.990000 0.000000 1.990000 ( 1.958056)
# => nil
ruby-1.9.2-p180:016:0>> quickbench(10**5) { h2(2323409845, 1782647144) }
Rehearsal ------------------------------------
0.340000 0.000000 0.340000 ( 0.333673)
--------------------------- total: 0.340000sec
user system total real
0.320000 0.000000 0.320000 ( 0.326854)
# => nil
ruby-1.9.2-p180:017:0>>
他のヒント
の提案mu合わせしろが短く書いて、簡単にCへの拡張を使__蔵_popcount、ベンチマークすることが検証では少なくとも3倍以上rubyの最適化した文字列関数..
また、以下の二つのチュートリアル:
私のプログラム:
require './FastPopcount/fastpopcount.so'
include FastPopcount
def hamming(a,b)
popcount(a^b)
end
その後、dirを含む私のプログラムでは、フォルダを作成する"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 作
その後はプログラムを、というわけなので....最速の方法でハミング距離rubyなどがある。
アルゴリズムのウェグナー:
def hamm_dist(a, b)
dist = 0
val = a ^ b
while not val.zero?
dist += 1
val &= val - 1
end
dist
end
p hamm_dist(2323409845, 1782647144) # => 17
れば取りに行く必要がない経路で、どちらが良いのかという追加のコンパイラフラグ -msse4.2
おmakefile.これによりコンパイラ生成ハードウェア popcnt
指を使わずにテーブルの生成にpopcount.自分のシステムの約2.5倍になります。