I have a binary number (52 bits) represented as a string "01100011...."
What would be the quickest way to count the number of 1's?
"01100011....".count("1")
obviously works but is quite time consuming if this operation needs to be done thousands of times.
ok, some more info. I am trying to create bit vectors for words as follows
def bit_vec(str)
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
bv = ""
alphabet.each_char do |a|
if str.include?(a)
bv += "1"
else
bv += "0"
end
end
bv
end
The bit_vec method gets called about 170K times. I store the bit vectors in a hash and use them to find similar words for a given word by XOR'ing the bit vectors and counting the number of 1's (more 1's == less similarity). If the count method does not use String#scan the what else could use it?
I know Ruby is slower than say C or Java. I am just looking to improve the algorithm the best I can. I am not looking for raw speed.
Maybe the include? method is the bottleneck?