From: Regis d'Aubarede Date: 2012-11-30T00:42:12+09:00 Subject: Re: Getting the smallest Items of an Array > def n_min(l,n) (1..n).map {a=l.min ; l=l-[a]; a } end > > array.sort[0, n] > n_min array, n You compar ruby implementation for n_min() with c implementation for sort... here result with a qsort from http://rosettacode.org, REP=10 class Array def qsort return self if length <= 1 pivot = self[0] less, greatereq = self[1..-1].partition { |x| x < pivot } less.qsort + [pivot] + greatereq.qsort end end Generating data... done user system total real 4/1024 items sort 0.031000 0.000000 0.031000 ( 0.029001) 4/1024 items n_min 0.000000 0.000000 0.000000 ( 0.006001) 4/131072 items sort 6.022000 0.016000 6.038000 ( 6.039345) 4/131072 items n_min 0.671000 0.016000 0.687000 ( 0.701040) 20/1024 items sort 0.031000 0.000000 0.031000 ( 0.028002) 20/1024 items n_min 0.031000 0.000000 0.031000 ( 0.027001) 20/131072 items sort 6.069000 0.031000 6.100000 ( 6.117349) 20/131072 items n_min 3.510000 0.078000 3.588000 ( 3.595206) 100/1024 items sort 0.031000 0.000000 0.031000 ( 0.030002) 100/1024 items n_min 0.125000 0.000000 0.125000 ( 0.135008) 100/131072 items sort 6.286000 0.000000 6.286000 ( 6.300360) 100/131072 items n_min 17.223000 0.265000 17.488000 ( 17.496001) -- Posted via http://www.ruby-forum.com/.