Sha256: a46ec2781f785e284c178af4adae4305e76f3215ee9b78be06eeec63d3a3959b
Contents?: true
Size: 1.05 KB
Versions: 5
Compression:
Stored size: 1.05 KB
Contents
module MemoryProfiler module TopN # Efficient mechanism for finding top_n entries in a list # optional block can specify custom element and weight def top_n(max = 10) sorted = if block_given? self.map { |row| yield(row) } else self.dup end sorted.compact! sorted.sort! found = [] last = sorted[0] count = 0 lowest_count = 0 sorted << nil sorted.each do |row| current_item, current_count = row unless current_item == last if count > lowest_count found << {data: last, count: count} end if found.length > max found.sort!{|x,y| x[:count] <=> y[:count] } found.delete_at(0) lowest_count = found[0][:count] end count = 0 last = current_item end count += (current_count || 1) unless row.nil? end found .sort!{|x,y| x[:count] <=> y[:count] } .reverse end end end
Version data entries
5 entries across 5 versions & 1 rubygems