循环中方法调用的开销是多少? [英] What is the overhead for a method call in a loop?

查看:54
本文介绍了循环中方法调用的开销是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#迷宫生成器已有一段时间了,它可以生成128000x128000像素的迷宫.所有内存使用情况都已经过优化,因此我目前正在考虑加快生成速度.

I’ve been working on a C# maze generator for a while which can generate mazes of like 128000x128000 pixels. All memory usage is optimized already so I’m currently looking at speeding the generation up.

我发现一个问题(远比一个兴趣点还重要)(以下仅是一些示例代码来说明该问题):

A problem (well more off an interest point) I found was the following (just some example code to illustrate the problem):

当pixelChanged为null时,此代码在我的计算机上运行约1.4秒:

This code runs in about 1.4 seconds on my machine when pixelChanged is null:

public void Go()
{
    for (int i = 0; i < bitjes.Length; i++)
    {
        BitArray curArray = bitjes[i];
        for (int y = 0; y < curArray.Length; y++)
        {
            curArray[y] = !curArray[y];
            GoDrawPixel(i, y, false);
        }
    }
}

public void GoDrawPixel(int i, int y, Boolean enabled)
{
    if (pixelChanged != null)
    {
        pixelChanged.Invoke(new PixelChangedEventArgs(i, y, enabled));
    }
}

以下代码的运行速度实际上快0.4秒

Where the following code runs actually 0.4 seconds faster

public void Go()
{
    for (int i = 0; i < bitjes.Length; i++)
    {
        BitArray curArray = bitjes[i];
        for (int y = 0; y < curArray.Length; y++)
        {
            curArray[y] = !curArray[y];
            if (pixelChanged != null)
            {
                pixelChanged.Invoke(new PixelChangedEventArgs(i, y, false));
            }
        }
    }
}

似乎当仅调用空"方法时,此算法使用的CPU大约占20%.这不是很奇怪吗?我曾尝试在调试和发布模式下编译该解决方案,但没有发现任何明显的差异.

It seems that when just calling an "empty" method takes about 20% of the cpu this algorithm uses. Isn’t this strange? I’ve tried to compile the solution in debug and release mode but haven’t found any noticeable differences.

这意味着我在此循环中进行的每个方法调用都会使我的代码速度降低约0.4秒.由于迷宫生成器代码当前由执行不同动作的许多单独的方法调用组成,因此这开始获得可观的收益.

What this means is that every method call I have in this loop will slow my code down by about 0.4 seconds. Since the maze generator code currently consist of a lot of seperate method calls that excecute different actions this starts to get a substantial ammount.

我还检查了Google和Stack Overflow上的其他帖子,但还没有真正找到解决方案.

I've also checked google and other posts on Stack Overflow but haven't really found a solution yet.

是否可以像这样自动优化代码?(也许是像Roslyn项目之类的东西???)还是我应该以一种大方法将所有内容放在一起?

Is it possible to automatically optimize code like this? (Maybe with things like project Roslyn???) or should I place everything together in one big method?

我还对这两种情况下的JIT/CLR代码差异进行一些分析感兴趣.(所以这个问题实际上是从哪里来的)

I'm also interested in maybe some analysis on the JIT/CLR code differences in these 2 cases. (So where this problem actually comes from)

Edit2:所有代码都是在发布模式下编译的

推荐答案

这是一个问题,JIT对方法进行了内联优化(其中整个方法代码实际上都注入了调用父代码中),但这仅在方法中发生被编译为32个字节或更少的字节.我不知道为什么存在32个字节的限制,并且我也想像在C/C ++中一样,在C#中看到一个内联"关键字,正是针对这些问题.

It is a problem, JIT has an inline optimization for methods (where the whole method code is actually injected inside the calling parent code) but this only happens for methods that are compiled to 32 bytes or less. I have no idea why the 32 byte limitation exists and would also like to see an 'inline' keyword in C# like in C/C++ exactly for these issues.

这篇关于循环中方法调用的开销是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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