#! /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); }