Sha256: 06308c729279209b9e8b7c028ba848dd6cfa301bff063db55ba34c997ab2fa16
Contents?: true
Size: 1.03 KB
Versions: 3
Compression:
Stored size: 1.03 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.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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
memory_profiler-0.0.4 | lib/memory_profiler/top_n.rb |
memory_profiler-0.0.3 | lib/memory_profiler/top_n.rb |
memory_profiler-0.0.2 | lib/memory_profiler/top_n.rb |