【发布时间】:2013-02-23 19:31:20
【问题描述】:
TestClassString 类返回一个 java.util.List of Strings
对象TestViewPerformance记录调用TestViewController.iterateList方法的时间。
在 iterateList 中,运行这个小程序所花费的时间在删除并行性后始终至少快 100 毫秒:
mySeq.par 至 我的序列
我意识到这里有用于测量 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;
}
}
【问题讨论】: