I need a slow C# function

前端 未结 6 2041
耶瑟儿~
耶瑟儿~ 2020-12-05 09:47

For some testing I\'m doing I need a C# function that takes around 10 seconds to execute. It will be called from an ASPX page, but I need the function to eat up CPU time on

6条回答
  •  独厮守ぢ
    2020-12-05 10:22

    For maxing out multiple cores I adjusted @Motti's answer a bit, and got the following:

    Enumerable
      .Range(1, Environment.ProcessorCount) // replace with lesser number if 100% usage is not what you are after.
      .AsParallel()
      .Select(i => {
        var end = DateTime.Now + TimeSpan.FromSeconds(10);
        while (DateTime.Now < end)
          /*nothing here */ ;
        return i;
      })
      .ToList(); // ToList makes the query execute.
    

提交回复
热议问题