ok, then tell me what is wrong with this microbench

    val idGenerator = Iterator from 0

    def loop(): Unit = {
      val t0 = System.nanoTime
      var n = 0
      while (n < 100000000) {
        idGenerator.next()
        n += 1
      }
      println("took " + ((System.nanoTime - t0) / 1000 / 1000) + " ms" + " (" + n + " -- " + idGenerator.next() + ")")
    }

    1 to 100 foreach {_ => loop()}


compared to

    var count = 0
    def nextId(): Int = {
      count += 1
      count
    }

    def loop2(): Unit = {
      val t0 = System.nanoTime
      var n = 0
      while (n < 100000000) {
        nextId()
        n += 1
      }
      println("took " + ((System.nanoTime - t0) / 1000 / 1000) + " ms" + " (" + n + " -- " + nextId() + ")")
    }

    1 to 100 foreach {_ => loop2()}

on my machine loop() takes ~ 240 ms and loop2() 8 ms
profiler tells me that loop() creates a lot of java.lang.Integer instances (boxing)