LINQ语句比“foreach”循环更快吗? [英] Is a LINQ statement faster than a 'foreach' loop?

查看:185
本文介绍了LINQ语句比“foreach”循环更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Mesh Rendering管理器,并认为将所有使用相同着色器的网格进行分组,然后在我处于该着色器循环时渲染这些网格将是一个好主意。



我正在使用一个 foreach 循环,但是想知道是否使用LINQ可能会使性能增加?为什么要LINQ要更快一些呢?它也在内部使用循环。

大多数情况下,LINQ会比较慢,因为它会引入开销。如果你关心性能,不要使用LINQ。使用LINQ是因为您希望更短的可读性和可维护性代码。


I am writing a Mesh Rendering manager and thought it would be a good idea to group all of the meshes which use the same shader and then render these while I'm in that shader pass.

I am currently using a foreach loop, but wondered if utilising LINQ might give me a performance increase?

解决方案

Why should LINQ be faster? It also uses loops internally.

Most of the times, LINQ will be a bit slower because it introduces overhead. Do not use LINQ if you care much about performance. Use LINQ because you want shorter better readable and maintainable code.

这篇关于LINQ语句比“foreach”循环更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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