4

I have the following data structures:

val set: scala.collection.immutable.Set[String] = ...
val test1: scala.collection.immutable.Map[String,scala.collection.immutable.Set[String]] = ...
val test2: Array[scala.collection.immutable.Set[String]] = ...

set contains about 60,000 entires. test1 has two entries ("one" and "two") and each is a Set of Strings similar to set. test2 is similar to test1 but the keys are 0 and 1 instead.

Running test1.get("one").get.contains("somestring") takes a long time (about 1 second) but running test2(0).contains("somestring") is very fast.

I don't quite understand why there is such a big difference. Any ideas?

4
  • 1
    If you assume that the map has "one" as an key entry, then you should call test1("one").contains("somestring")
    – Kigyo
    Dec 18, 2013 at 12:42
  • @Kigyo Thanks for the note, it definitely looks better. However, the apply method has the same performance as the get method in my test. Dec 18, 2013 at 12:57
  • Like others said: We need more information regarding your code.
    – Kigyo
    Dec 18, 2013 at 13:26
  • How can we answer this question if you don't paste the actual implementation used for test1? Can't guess much from an interface...
    – vptheron
    Dec 18, 2013 at 13:58

3 Answers 3

3

The problem was that I was using mapValues on an existing Map to generate the new Map. I thought mapValues works similarly to map but in reality mapValues only creates a View on the existing Map instead of a new Map.

1

This:

test2(0)

Runs really fast because it's an array, it knows exactly where 0 is, the map has to find the "one" key first and then get's it's pair.

4
  • It's obvious that an Array is faster than a Map, the question is why the Map is so tremendously slow, even though it only has two entries. It takes much more time than the contains lookup in the Set with 60,000 entries. Dec 18, 2013 at 12:52
  • 1
    And how are you benchmarking this? I can't measure the time to run map("one") using System.currentTimeInMillis(). It's always zero. Dec 18, 2013 at 13:04
  • @flavian he said he's seeing a one second difference. And with only two keys the worst case scenario is two comparisons inside a hash bucket, the amount of operations is negligible. Dec 18, 2013 at 13:13
  • Sorry guys, I did not provide the full problem details! After more testing I've found that the problem was that I created the Map using the mapValues method, and I did not know that it only created a View on the old Map instead of a new Map. Sorry for wasting your time. :( Dec 18, 2013 at 13:31
1

Running this code to generate some collections like you mentioned:

import scala.collection.mutable.{HashSet, HashMap}
import scala.util.Random

def genSet(count: Int = 100 * 1000, stringSize: Int = 10): Set[String] = {
  val set = new HashSet[String]
  set.sizeHint(count)

  for(i <- 1 to count) {
    set.add(i.toString)
  }

  set.toSet
} 

def genSetMap(count: Int = 2, keySize: Int = 10)
             (f: => Set[String]): Map[String, Set[String]] = {
  val map = new HashMap[String, Set[String]]
  map.sizeHint(count)

  for(i <- 1 to count) {
    map.put(i.toString, genSet())
  }

  map.toMap
}

The following test, using 100.000 elements per set, still runs instantly:

val map = genSetMap(2, 10){ genSet(100*1000) }
map("2").contains("99999") // res2: Boolean = true

So I suspect there is some peculiarity in your actual code that is causing it not to generate a set, but some other intermediate collection that doesn't have fast searching. Can you provide a more concrete example of the actual code you are seeing the behavior with?

1
  • Thanks for your contribution. You are right, the problem was in the code that was not part of the question. The Map I was using was created using the mapValues method which only creates a View on the old Map instead of a new Map. Dec 18, 2013 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.