跟踪进度最有效的Ruby数据结构是什么?
我正在做一个小项目,它逐步增加链接列表,然后通过队列处理它们。一个链接可能会被输入队列两次,我想跟踪我的进度,这样我就可以跳过已经处理过的任何内容。我估计最多有10万个独特的链接 对于更大的项目,我会使用数据库,但对于我正在处理的数据量来说,这似乎有些过头了。如果我想保存运行过程中的进度,我更喜欢某种形式的内存解决方案,这种解决方案可能会被序列化 什么样的数据结构最适合这种需要 更新:我已经在使用哈希来跟踪我已完成处理的链接。这是最有效的方法吗跟踪进度最有效的Ruby数据结构是什么?,ruby,performance,data-structures,Ruby,Performance,Data Structures,我正在做一个小项目,它逐步增加链接列表,然后通过队列处理它们。一个链接可能会被输入队列两次,我想跟踪我的进度,这样我就可以跳过已经处理过的任何内容。我估计最多有10万个独特的链接 对于更大的项目,我会使用数据库,但对于我正在处理的数据量来说,这似乎有些过头了。如果我想保存运行过程中的进度,我更喜欢某种形式的内存解决方案,这种解决方案可能会被序列化 什么样的数据结构最适合这种需要 更新:我已经在使用哈希来跟踪我已完成处理的链接。这是最有效的方法吗 def process_link(link)
def process_link(link)
return if @processed_links[link]
# ... processing logic
@processed_links[link] = Time.now # or other state
end
如何设置并将链接转换为值对象(而不是引用对象),如结构。通过创建值对象,集合将能够检测其唯一性。或者,您可以使用散列并按其主键存储链接。设置并将链接转换为值对象(而不是引用对象),如结构。通过创建值对象,集合将能够检测其唯一性。或者,您可以使用散列并按其主键存储链接。设置并将链接转换为值对象(而不是引用对象),如结构。通过创建值对象,集合将能够检测其唯一性。或者,您可以使用散列并按其主键存储链接。设置并将链接转换为值对象(而不是引用对象),如结构。通过创建值对象,集合将能够检测其唯一性。或者,您可以使用散列并按其主键存储链接。数据结构可以是散列:
current_status = { links: [link3, link4, link5], processed: [link1, link2, link3] }
要跟踪您的进度(百分比):
要处理您的链接,请执行以下操作:
将需要处理的任何新链接推送到
李>当前状态[:links]
- 使用
从shift
获取下一个要处理的链接当前状态[:links]
- 处理链接后,
将其推送到
当前状态[:已处理]
- 使用
# Add any new link that needs to be processed to the queue unless it have been processed
def add_link_to_queue(link)
current_status[:to_process].push(link) unless current_status[:processed].include?(link)
end
# Process next link on the queue
def process_next_link
link = current_status[:to_process].shift # return first link on the queue
# ... login process the link
current_status[:processed].push(link)
end
# shift method will not only return but also remove the link from the original array to avoid duplications
数据结构可以是散列:
current_status = { links: [link3, link4, link5], processed: [link1, link2, link3] }
要跟踪您的进度(百分比):
要处理您的链接,请执行以下操作:
将需要处理的任何新链接推送到
李>当前状态[:links]
- 使用
从shift
获取下一个要处理的链接当前状态[:links]
- 处理链接后,
将其推送到
当前状态[:已处理]
- 使用
# Add any new link that needs to be processed to the queue unless it have been processed
def add_link_to_queue(link)
current_status[:to_process].push(link) unless current_status[:processed].include?(link)
end
# Process next link on the queue
def process_next_link
link = current_status[:to_process].shift # return first link on the queue
# ... login process the link
current_status[:processed].push(link)
end
# shift method will not only return but also remove the link from the original array to avoid duplications
数据结构可以是散列:
current_status = { links: [link3, link4, link5], processed: [link1, link2, link3] }
要跟踪您的进度(百分比):
要处理您的链接,请执行以下操作:
将需要处理的任何新链接推送到
李>当前状态[:links]
- 使用
从shift
获取下一个要处理的链接当前状态[:links]
- 处理链接后,
将其推送到
当前状态[:已处理]
- 使用
# Add any new link that needs to be processed to the queue unless it have been processed
def add_link_to_queue(link)
current_status[:to_process].push(link) unless current_status[:processed].include?(link)
end
# Process next link on the queue
def process_next_link
link = current_status[:to_process].shift # return first link on the queue
# ... login process the link
current_status[:processed].push(link)
end
# shift method will not only return but also remove the link from the original array to avoid duplications
数据结构可以是散列:
current_status = { links: [link3, link4, link5], processed: [link1, link2, link3] }
要跟踪您的进度(百分比):
要处理您的链接,请执行以下操作:
将需要处理的任何新链接推送到
李>当前状态[:links]
- 使用
从shift
获取下一个要处理的链接当前状态[:links]
- 处理链接后,
将其推送到
当前状态[:已处理]
- 使用
# Add any new link that needs to be processed to the queue unless it have been processed
def add_link_to_queue(link)
current_status[:to_process].push(link) unless current_status[:processed].include?(link)
end
# Process next link on the queue
def process_next_link
link = current_status[:to_process].shift # return first link on the queue
# ... login process the link
current_status[:processed].push(link)
end
# shift method will not only return but also remove the link from the original array to avoid duplications
如果您不关心内存,那么只需使用散列来检查包含;插入和查找时间为O(1)个平均值。序列化是直接的(Ruby的封送处理类应该为您解决这个问题,或者您可以使用类似JSON的格式)。Ruby的
Set
是一个类似数组的对象,后面有一个散列,所以如果您愿意,可以直接使用它
然而,如果内存是一个问题,那么这对一个年轻人来说是一个很大的问题!您可以在固定时间内实现集合包含测试,并且过滤器使用的内存比散列要少得多。折衷的办法是,Bloom过滤器是概率性的——您可以得到假包含阳性。使用正确的bloom filter参数可以消除大多数误报的可能性,但如果重复是例外而不是规则,则可以实现如下功能:
是我过去使用过的Bloom过滤器实现;它工作得很好。如果您需要能够跨多个应用程序实例执行集合成员身份跟踪和测试的功能,还可以使用redis支持的Bloom过滤器。如果您不关心内存,那么只需使用哈希来检查是否包含;插入和查找时间为O(1)个平均值。序列化是直接的(Ruby的封送处理类应该为您解决这个问题,或者您可以使用f