我的部分研究项目是使用秒表来测量时间。
每个秒表用于不同的任务,以测量 while 循环应该工作多长时间。
我想知道如果我放了很多长任务,这个度量会准确吗?
想法代码示例:
public static void run(){
var tasks= new List<Task<int>>();
foreach(var task in taskList) //taskList is global object with 10 variables inside
tasks.Add(Task.Factory.StartNew<int>(()=>taskFunction(task)));
Task.WaitAll(tasks.ToArray());
}
private int taskFunction(task) {
Stopwatch watch = new Stopwatch();
while (watch.ElapsedMilliseconds < MaxTime * 1000)
{
watch.Start();
doSomething(); // takes lot of time;
watch.Stop();
doSomethingDiffrent();
}
}
所以我将创建 10 个任务。如果任务不是由处理器计算的,那么秒表中的时间会运行并浪费吗?
慕妹3242003
浮云间
相关分类