in reply to top 20 song hits
For perl, there is Array::Heap, which ought to perform adequately.
This following does 1M counts + 1000 toplists in a second or two.
(Update: performance comparisons with a database are welcome.)
#! /usr/bin/perl -l use Array::Heap; print "starting"; my @h = map [-rand, "item $_"], 'aa000' .. 'zz999'; print int(@h), " elements"; make_heap @h; print "heap made"; sub hits { --$h[$_][0], adjust_heap @h,$_ for @_; } sub batch { hits map {int @h*rand()**4} 1..$_[0]; } sub rotate { $_->[0] *= rand, $_->[1] .= '.', push_heap @h,$_ for map {pop_heap @h} 1..$_[0]; } sub top { map sprintf("%8d %s", -$_->[0], $_->[1]), map {push_heap @h,$_;$_} map {pop_heap @h} 1..$_[0]; } for (;;) { batch(1000), top(10) for 1..1000; print for "", top(10); rotate(3); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: top 20 song hits
by davido (Cardinal) on Dec 02, 2013 at 01:20 UTC |