代码之家  ›  专栏  ›  技术社区  ›  Dr Y Wit

不变性和性能

  •  -1
  • Dr Y Wit  · 技术社区  · 8 年前

    我尝试搜索stackoverflow,有很多相关的主题,包括

    val-mutable versus var-immutable in Scala

    What is the difference between a var and val definition in Scala?

    但我还是想澄清一下我的理解是否正确。

    规则如下

    不可变val优于不可变var,可变val优于可变val。 变量,尤其是可变变量上的不可变变量!

    但不可变变量的性能比可变变量差得多。 repl中的简单测试

    scala -J-Xmx2g
    
    scala> import scala.collection.mutable.{Map => MMap}
    import scala.collection.mutable.{Map=>MMap}
    
    scala>
    
    scala> def time[R](block: => R): R = {
         |   val t0 = System.nanoTime()
         |   val result = block    // call-by-name
         |   val t1 = System.nanoTime()
         |   println("Elapsed time: " + (t1 - t0) + "ns")
         |   result
         | }
    time: [R](block: => R)R
    
    scala>time {val mut_val = MMap[Int, Int](); for (i <- 1 to 1000000) mut_val += (i -> i)}
    Elapsed time: 551073900ns
    
    scala>time {var mut_var = MMap[Int, Int](); for (i <- 1 to 1000000) mut_var += (i -> i)}
    Elapsed time: 574174400ns
    
    scala>time {var imut_var = Map[Int, Int](); for (i <- 1 to 1000000) imut_var += (i -> i)}
    Elapsed time: 860938800ns
    
    scala>time {val mut_val = MMap[Int, Int](); for (i <- 1 to 2000000) mut_val += (i -> i)}
    Elapsed time: 1103283000ns
    
    scala>time {var mut_var = MMap[Int, Int](); for (i <- 1 to 2000000) mut_var += (i -> i)}
    Elapsed time: 1166532600ns
    
    scala>time {var imut_var = Map[Int, Int](); for (i <- 1 to 2000000) imut_var += (i -> i)}
    Elapsed time: 2926954500ns
    

    我还补充了可变的var,甚至很难理解。 它的性能与可变val非常相似,只是为了完成这张图片。 但不可变VaR的性能要差得多。

    因此,不变var与可变val的代价是性能下降(以及更广泛的内存使用!).有人能再解释一下支付这个价格的意义吗?请举出具体的例子。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   kag0    8 年前


    TIMEOUT_1 t1 PARALLEL_FACTOR_1 pf1 TIMEOUT_2 t2

    var defaultConfig = immutable.Map("timeout" → "5", "parallelFactor" → "4")
    var worker1Config = defaultConfig
    var worker2Config = defaultConfig
    
    worker1Config += "timeout" → sys.env("TIMEOUT_1")
    worker1Config += "parallelFactor" → sys.env("PARALLEL_FACTOR_1")
    
    worker2Config += "timeout" → sys.env("TIMEOUT_2")
    worker2Config += "parallelFactor" → sys.env("PARALLEL_FACTOR_2")
    
    println(defaultConfig) // Map(timeout -> 5, parallelFactor -> 4)
    println(worker1Config) // Map(timeout -> t1, parallelFactor -> pf1)
    println(worker2Config) // Map(timeout -> t2, parallelFactor -> pf2)
    

    val defaultConfig = mutable.Map("timeout" → "5", "parallelFactor" → "4")
    val worker1Config = defaultConfig
    val worker2Config = defaultConfig
    
    worker1Config += "timeout" → sys.env("TIMEOUT_1")
    worker1Config += "parallelFactor" → sys.env("PARALLEL_FACTOR_1")
    
    worker2Config += "timeout" → sys.env("TIMEOUT_2")
    worker2Config += "parallelFactor" → sys.env("PARALLEL_FACTOR_2")
    
    println(defaultConfig) // Map(parallelFactor -> pf2, timeout -> t2)
    println(worker1Config) // Map(parallelFactor -> pf2, timeout -> t2)
    println(worker2Config) // Map(parallelFactor -> pf2, timeout -> t2)
    

    推荐文章