Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the Nth key in a TreeMap most efficiently
    primarykey
    data
    text
    <p>I have a Scala TreeMap that automatically sorts the keys. What I would like to know is if there is a more performant way of finding the Nth key in the map than the following example:</p> <pre><code>treeMap.take(N).lastKey </code></pre> <p>Thanks, Bruce</p> <p><strong>EDIT:</strong><br> I created a small test using the following code:</p> <pre><code>class Test { var treeMap = new scala.collection.immutable.TreeMap[Double,String]() val numberOfEntries = 1000 (0 until numberOfEntries) map { i =&gt; {treeMap += {i.toDouble -&gt; i.toString}}} val iterations = 2000 var N = 1 while(N &lt; numberOfEntries) { // my original version var i = 0 val start1 = System.nanoTime() while(i &lt; iterations) { i += 1 val v = treeMap.take(N).lastKey } val end1 = System.nanoTime() val elapsed1 = end1 - start1 // Daniel's suggestion i = 0 val start2 = System.nanoTime() while(i &lt; iterations) { i += 1 val v = treeMap.keysIterator.drop(N - 1).next } val end2 = System.nanoTime() val elapsed2 = end2 - start2 println("N = %d, elapsed1 = %d, elapsed2 = %d".format(N,elapsed1,elapsed2)) N += 50 } } object Test { def main(args:Array[String]) { val test = new Test } } </code></pre> <p>It appears that Daniel's suggestion is indeed better</p> <p><strong>Results</strong></p> <pre><code>N = 1, elapsed1 = 956492000, elapsed2 = 700300000 N = 51, elapsed1 = 1103271000, elapsed2 = 936045000 N = 101, elapsed1 = 1286896000, elapsed2 = 1041744000 N = 151, elapsed1 = 1368854000, elapsed2 = 1199766000 N = 201, elapsed1 = 1584878000, elapsed2 = 1333284000 N = 251, elapsed1 = 1790965000, elapsed2 = 1468806000 N = 301, elapsed1 = 2052298000, elapsed2 = 1649021000 N = 351, elapsed1 = 2294625000, elapsed2 = 1819525000 N = 401, elapsed1 = 2529855000, elapsed2 = 1961699000 N = 451, elapsed1 = 2762582000, elapsed2 = 2100127000 N = 501, elapsed1 = 2977613000, elapsed2 = 2232108000 N = 551, elapsed1 = 3211812000, elapsed2 = 2384940000 N = 601, elapsed1 = 3437116000, elapsed2 = 2539431000 N = 651, elapsed1 = 3652749000, elapsed2 = 2650910000 N = 701, elapsed1 = 3900431000, elapsed2 = 2807085000 N = 751, elapsed1 = 4123141000, elapsed2 = 2934904000 N = 801, elapsed1 = 4337909000, elapsed2 = 3060158000 N = 851, elapsed1 = 4554490000, elapsed2 = 3188378000 N = 901, elapsed1 = 4768488000, elapsed2 = 3306528000 N = 951, elapsed1 = 4978839000, elapsed2 = 3413813000 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload