How to optimize this short factorial function in scala? (Creating 50000 BigInts)

前端 未结 4 1366
名媛妹妹
名媛妹妹 2021-02-13 06:41

I\'ve compaired the scala version

(BigInt(1) to BigInt(50000)).reduce(_ * _)

to the python version

reduce(lambda x,y: x*y, rang         


        
4条回答
  •  一整个雨季
    2021-02-13 07:40

    Python on my machine:

    def func():
      start= time.clock()
      reduce(lambda x,y: x*y, range(1,50000))
      end= time.clock()
      t = (end-start) * 1000
      print t
    

    gives 1219 ms

    Scala:

    def timed[T](f: => T) = {
      val t0 = System.currentTimeMillis
      val r = f
      val t1 = System.currentTimeMillis
      println("Took: "+(t1 - t0)+" ms")
      r
    }
    
    timed { (BigInt(1) to BigInt(50000)).reduce(_ * _) }
    4251 ms
    
    timed { (BigInt(1) to BigInt(50000)).fold(BigInt(1))(_ * _) }
    4224 ms
    
    timed { (BigInt(1) to BigInt(50000)).par.reduce(_ * _) }
    2083 ms
    
    timed { (BigInt(1) to BigInt(50000)).par.fold(BigInt(1))(_ * _) }
    689 ms
    
    // using org.jscience.mathematics.number.LargeInteger from Travis's answer
    timed { val a = (1 to 50000).foldLeft(LargeInteger.ONE)(_ times _) }
    3327 ms
    
    timed { val a = (1 to 50000).map(LargeInteger.valueOf(_)).par.fold(
                                              LargeInteger.ONE)(_ times _) }
    361 ms
    

    This 689 ms and 361 ms were after a few warmup runs. They both started at about 1000 ms, but seem to warm up by different amounts. The parallel collections seem to warm up significantly more than the non-parallel: the non-parallel operations did not reduce significantly from their first runs.

    The .par (meaning, use parallel collections) seemed to speed up fold more than reduce. I only have 2 cores, but a greater number of cores should see a bigger performance gain.

    So, experimentally, the way to optimize this function is

    a) Use fold rather than reduce

    b) Use parallel collections

    update: Inspired by the observation that breaking the calculation down into smaller chunks speeds things up, I managed to get he following to run in 215 ms on my machine, which is a 40% improvement on the standard parallelized algorithm. (Using BigInt, it takes 615 ms.) Also, it doesn't use parallel collections, but somehow uses 90% CPU (unlike for BigInt).

      import org.jscience.mathematics.number.LargeInteger
    
      def fact(n: Int) = {
        def loop(seq: Seq[LargeInteger]): LargeInteger = seq.length match {
          case 0 => throw new IllegalArgumentException
          case 1 => seq.head
          case _ => loop {
            val (a, b) = seq.splitAt(seq.length / 2)
            a.zipAll(b, LargeInteger.ONE, LargeInteger.ONE).map(i => i._1 times i._2)
          } 
        }
        loop((1 to n).map(LargeInteger.valueOf(_)).toIndexedSeq)
      }
    

提交回复
热议问题