类 TestClassString 返回一个 java.util.List of Strings
TestViewPerformance 对象记录了调用TestViewController.iterateList 方法所花费的时间。
在 iterateList 中,当并行性被移除时,运行这个小程序所花费的时间至少快 100 毫秒:
mySeq.par 到 mySeq
我意识到这里有用于测量 scala 性能的基准测试工具:http: //docs.scala-lang.org/overviews/parallel-collections/performance.html
但是我仍然希望这个程序使用基于当前毫秒时间的并行性运行得更快吗?.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;
}
}