在 Ruby 的 NArray 中对设置位求和的最快方法是什么?
我使用NArray来实现位数组,但我对bits_on方法的速度不太满意。目前我有:
# Method that returns the number of bits set "on" in a bit array.
def bits_on
bits_on = 0
self.byte_array.each do |byte|
bits_on += @count_array[byte]
end
bits_on
end
byte_array
是 NArray.byte() 类型,而 @count_array
的构建如下:
# Method that returns an array where the element index value is
# the number of bits set for that index value.
def init_count_array
count_array = []
(0 ... (2 ** BitsInChar)).each do |i|
count_array << bits_in_char(i)
end
count_array
end
有想法吗?
干杯,
马丁
I used NArray to implement a bit array, but I am not quite satisfied with the speed of the bits_on method. Currently I have:
# Method that returns the number of bits set "on" in a bit array.
def bits_on
bits_on = 0
self.byte_array.each do |byte|
bits_on += @count_array[byte]
end
bits_on
end
byte_array
is an NArray.byte() type, and @count_array
is build like this:
# Method that returns an array where the element index value is
# the number of bits set for that index value.
def init_count_array
count_array = []
(0 ... (2 ** BitsInChar)).each do |i|
count_array << bits_in_char(i)
end
count_array
end
Ideas?
Cheers,
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否正确理解了背景,一个可能的解决方案是:
抱歉,上面是错误的,下一个将起作用:
I am not sure I understand the background correctly, a possible solution is:
Sorry, the above is wrong, the next will work: