Why does C# execute Math.Sqrt() more slowly than VB.NET?

后端 未结 6 1550
情深已故
情深已故 2020-12-14 05:45

Background

While running benchmark tests this morning, my colleagues and I discovered some strange things concerning performance of C# code vs. VB.NET code.

6条回答
  •  爱一瞬间的悲伤
    2020-12-14 06:04

    The difference is in the loop; your C# code is computing the square root on every iteration. Changing that one line from:

    for (Int32 i = 2; i <= Math.Sqrt(suspectPrime); i++) {
    

    to:

    var lim = Math.Sqrt(suspectPrime);
    for (Int32 i = 2; i <= lim; i++) {
    

    dropped the time on my machine from 26 seconds to 7.something.

提交回复
热议问题