Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在这种情况下,为什么使用scala并行会降低性能?_Java_Scala - Fatal编程技术网

Java 在这种情况下,为什么使用scala并行会降低性能?

Java 在这种情况下,为什么使用scala并行会降低性能?,java,scala,Java,Scala,TestClassString类返回字符串的java.util.List 对象TestViewPerformance记录调用方法TestViewController.iterateList所花费的时间 在iterateList中,当删除并行性时,运行此小程序所需的时间始终至少快100毫秒: P.MySEQ.PAR 到 迈赛克 我意识到有一种基准测试工具用于测量scala性能,具体如下: 但我仍然希望这个程序在当前毫秒时间的基础上使用并行性运行得更快?在PAR循环内的所有代码是否遍布多个内核?

TestClassString类返回字符串的java.util.List

对象TestViewPerformance记录调用方法TestViewController.iterateList所花费的时间

在iterateList中,当删除并行性时,运行此小程序所需的时间始终至少快100毫秒:

P.MySEQ.PAR 到 迈赛克

我意识到有一种基准测试工具用于测量scala性能,具体如下:

但我仍然希望这个程序在当前毫秒时间的基础上使用并行性运行得更快?在PAR循环内的所有代码是否遍布多个内核?

以下是完整的代码:

package testpackage

import java.util.Calendar

object TestViewPerformance {

  def main(args:Array[String]) = {

      val before = Calendar.getInstance().getTimeInMillis()

      val testViewController = new TestViewController();
      val testClassString : TestClassString = new TestClassString()

      val folderList = testClassString.getStringList()
      var buffer = new scala.collection.mutable.ListBuffer[String]
      val seq = scala.collection.JavaConversions.asScalaBuffer(folderList);

      /*
       * this method (iterateList) is where the parallelism occurs
       */
      testViewController.iterateList(seq)

      val after = Calendar.getInstance().getTimeInMillis()

      println(before)
      println(after)
      println(after-before)

  }

  class TestViewController {

      def iterateList(mySeq : Seq[String]) = {

        for (seqVal<- mySeq) {
            if(seqVal.equalsIgnoreCase("test")){            

            }
        }   
}

}

}

package testpackage;

import java.util.ArrayList;
import java.util.List;

public class TestClassString {

    public List<String> getStringList(){

        List<String> l = new ArrayList<String>();

        for(int i = 0; i < 1000000; ++i){
            String test = ""+Math.random();
            l.add(test);
        } 

        return l;
    }

}
package-testpackage
导入java.util.Calendar
对象TestViewPerformance{
def main(参数:数组[字符串])={
val before=Calendar.getInstance().getTimeInMillis()
val testViewController=新的testViewController();
val testClassString:testClassString=newtestclassstring()
val folderList=testClassString.getStringList()
var buffer=new scala.collection.mutable.ListBuffer[String]
val seq=scala.collection.JavaConversions.asScalaBuffer(folderList);
/*
*这个方法(iterateList)是并行性发生的地方
*/
testViewController.iterateList(seq)
val after=Calendar.getInstance().getTimeInMillis()
println(之前)
println(之后)
println(在之前之后)
}
类TestViewController{
def iterateList(mySeq:Seq[String])={

对于(seqVal这可能是因为每次迭代的大部分时间都花在打印到
System.out
,这是一个不可并行化的同步操作。因此,启动线程、调度线程和同步线程所产生的成本使得并行迭代比顺序迭代慢。

因为您的基准测试s测量线程切换和量子波动的开销。将至少
thread.sleep(1)
添加到您的循环中,看看会发生什么:

scala> val strings = (1 to 10000).map(_ + Math.random().toString)
strings: scala.collection.immutable.IndexedSeq[String] = Vector(10.8907863042670979, 20.2871957696184603, 30.20011325237932742, 40.7490949002788928, 50.5073228980632211...
scala> val time = System.currentTimeMillis; 
       | for (str <- strings.par) {Thread.sleep(1)}; 
       | System.currentTimeMillis - time
res0: Long = 1398

scala> val time = System.currentTimeMillis; 
       | for (str <- strings) {Thread.sleep(1)}; 
       | System.currentTimeMillis - time
res3: Long = 11129
scala>val strings=(1到10000).map(uz+Math.random().toString)
字符串:scala.collection.immutable.IndexedSeq[String]=Vector(10.8907863042670979,20.2871957696184603,30.20011325237932742,40.7490949002788928,50.507322898063221。。。
scala>val time=System.currentTimeMillis;
|对于(str val time=System.currentTimeMillis;

|对于(str)我删除了println,但结果相同,“if条件”永远不会为真。让每次迭代都做一些实质性的事情(或休眠一段时间)。比较两个长度或第一个字符总是不同的字符串非常快,在1000000个实例上并行化它不会带来任何性能提升。添加线程。休眠(1)使它运行得更快。我对为什么会感到困惑。在生产中,没有“线程-睡眠”,这是否意味着我不应该使用PAR,因为它似乎没有它运行得更快?@ USS470184:在生产中,您将有一些处理,并且可能需要超过一毫秒。您必须理解并行集合引起一些OV。将任务分割为线程,管理它们等。因此,如果在循环中所做的任何处理小于此开销,则不值得使用PAR。