为什么一百万个double-int转换的执行时间与一个空循环相同? [英] Why the execution time of a million double-int conversion is the same as an empty loop?

查看:29
本文介绍了为什么一百万个double-int转换的执行时间与一个空循环相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个具有很多int-double-int转换的高性能组件,因此我需要知道它们之间的执行时间.

I'm writing a high-performance component with a lot of int-double-int conversions, so I need to know the execution time between them.

static double ToDouble(int val) => (double)val;

static int ToInt(double val) => (int)val;

static void Main(string[] args) {
    const int TIMES = 1000_0000;
    Console.ReadLine();

    var t_0 = Stopwatch.GetTimestamp();
    for (int i = 0; i < TIMES; i++) {
        var val = ToInt(ToDouble(i));
    }
    var t_1 = Stopwatch.GetTimestamp();
    Console.WriteLine((t_1 - t_0) * 100_0000L / Stopwatch.Frequency); // 4002 Microseconds


    var t_2 = Stopwatch.GetTimestamp();
    for (int i = 0; i < TIMES; i++) {

    }
    var t_3 = Stopwatch.GetTimestamp();
    Console.WriteLine((t_3 - t_2) * 100_0000L / Stopwatch.Frequency); // 3997 Microseconds


    Console.ReadLine();
}

我发现int-double-int转换是如此之快,以至于执行时间与空循环相当.

I found that int-double-int conversion is so fast that execution time is comparable to empty loops.

我认为第一个循环中的代码根本不执行,编译器将其优化为空循环,是吗?

I think the code in the first loop is not executed at all, it is optimized by the compiler as an empty loop, is that true?

推荐答案

您应该使用StopWatch.Start和Stop然后已过去!

You should use StopWatch.Start and Stop then Elapsed!

const int TIMES = 100_000_000;

var chrono = new Stopwatch();
int val = 0;

chrono.Start();
for ( int i = 1; i <= TIMES; i++ )
  val = ToInt(ToDouble(i));
chrono.Stop();
Console.WriteLine(val);
Console.WriteLine(chrono.ElapsedMilliseconds.ToString());

chrono.Restart();
for ( int i = 1; i <= TIMES; i++ )
{
  var v1 = (double)i;
  val = (int)v1;
}
chrono.Stop();
Console.WriteLine(val);
Console.WriteLine(chrono.ElapsedMilliseconds.ToString());

chrono.Restart();
for ( int i = 1; i <= TIMES; i++ )
  val = i;
chrono.Stop();
Console.WriteLine(val);
Console.WriteLine(chrono.ElapsedMilliseconds.ToString());

chrono.Restart();
for ( int i = 1; i <= TIMES; i++ )
  ;
chrono.Stop();
Console.WriteLine(chrono.ElapsedMilliseconds.ToString());

以调试模式输出:

729
270
194
218

使用发布版本进行了优化:

Using Release build optimized:

84
61
57
31

Debug IL中的第一个循环:

The first loop in Debug IL:

  // value = ToInt(ToDouble(i));
  IL_0015: ldloc.2
  IL_0016: call float64 ConsoleApp.Program::ToDouble(int32)
  IL_001b: call int32 ConsoleApp.Program::ToInt(float64)
  IL_0020: stloc.1

版本IL中的第一个循环:

The first loop in Release IL:

  // value = ToInt(ToDouble(i));
  IL_0012: ldloc.2
  IL_0013: call float64 ConsoleApp.Program::ToDouble(int32)
  IL_0018: call int32 ConsoleApp.Program::ToInt(float64)
  IL_001d: stloc.1

第二个循环Debug:

The second loop Debug:

  // double num = j;
  IL_0065: ldloc.s 5
  IL_0067: conv.r8
  IL_0068: stloc.s 6
  // value = (int)num;
  IL_006a: ldloc.s 6
  IL_006c: conv.i4
  IL_006d: stloc.1

第二个循环Release:

The second loop Release:

  // value = (int)(double)j;
  IL_0054: ldloc.s 4
  IL_0056: conv.r8
  IL_0057: conv.i4
  IL_0058: stloc.1

Proc调用占用大量CPU时钟,这是使用循环和计算进行优化时要考虑的第一件事.

Proc calls eat a lot of CPU ticks and this is the first thing to consider when optimizing, with loops and calculation.

编译器优化主要是循环本身:

The compiler optimization is mainly with the loop itself:

  • 调试:
// for (int i = 1; i <= 100000000; i++)
IL_0010: ldc.i4.1
IL_0011: stloc.2
// (no C# code)
IL_0012: br.s IL_0026
// loop start (head: IL_0026)
  //...
  // for (int i = 1; i <= 100000000; i++)
  IL_0022: ldloc.2
  IL_0023: ldc.i4.1
  IL_0024: add
  IL_0025: stloc.2
  // for (int i = 1; i <= 100000000; i++)
  IL_0026: ldloc.2
  IL_0027: ldc.i4 100000000
  IL_002c: cgt
  // (no C# code)
  IL_002e: ldc.i4.0
  IL_002f: ceq
  IL_0031: stloc.3
  IL_0032: ldloc.3
  IL_0033: brtrue.s IL_0014
// end loop

  • 发布:
  • // for (int i = 1; i <= 100000000; i++)
    IL_000c: ldc.i4.1
    IL_000d: stloc.1
    // (no C# code)
    IL_000e: br.s IL_0020
    // loop start (head: IL_0020)
      // ...
      // for (int i = 1; i <= 100000000; i++)
      IL_001c: ldloc.1
      IL_001d: ldc.i4.1
      IL_001e: add
      IL_001f: stloc.1
      // for (int i = 1; i <= 100000000; i++)
      IL_0020: ldloc.1
      IL_0021: ldc.i4 100000000
      IL_0026: ble.s IL_0010
    // end loop
    

    没有console.writeline(val)的调试循环:

    The loops in debug without the console.writeline(val):

      // value = ToInt(ToDouble(i));
      IL_0015: ldloc.2
      IL_0016: call float64 ConsoleApp.Program::ToDouble(int32)
      IL_001b: call int32 ConsoleApp.Program::ToInt(float64)
      IL_0020: stloc.1
    
      // double num = j;
      IL_0065: ldloc.s 5
      IL_0067: conv.r8
      IL_0068: stloc.s 6
      // value = (int)num;
      IL_006a: ldloc.s 6
      IL_006c: conv.i4
      IL_006d: stloc.1
    
      // value = k;
      IL_00b7: ldloc.s 8
      IL_00b9: stloc.1
    
      // nothing
    

    发行版中没有console.writeline(val)的循环:

    The loops in release without the console.writeline(val):

      // ToInt(ToDouble(i));
      IL_0010: ldloc.1
      IL_0011: call float64 ConsoleApp.Program::ToDouble(int32)
      IL_0016: call int32 ConsoleApp.Program::ToInt(float64)
      IL_001b: pop
    
      // _ = (double)j;
      IL_004b: ldloc.3
      IL_004c: conv.r8
      IL_004d: pop
    
      // nothing
    
      // nothing
    

    这篇关于为什么一百万个double-int转换的执行时间与一个空循环相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆