是否有呼叫。长度数组与保存大小可变的性能差异? [英] Is there a difference in performance for calling .length on an array versus saving a size variable?

查看:142
本文介绍了是否有呼叫。长度数组与保存大小可变的性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个模拟程序,我想在code将非常优化。现在我有一个被通过了很多,在不同的循环为我用

I am creating a simulation program, and I want the code to be very optimized. Right now I have an array that gets cycled through a lot and in the various for loops I use

 for(int i = 0; i<array.length; i++){
       //do stuff with the array
 }

我在想,如果,如​​果我在课堂上保存一个变量来指定这个数组的长度,并用该相反,它会更快。或者,如果它在所有。

I was wondering if it would be faster if I saved a variable in the class to specify this array length, and used that instead. Or if it matters at all.

推荐答案

访问长度属性的阵列上是一样快,因为它得到。

Accessing the length attribute on an array is as fast as it gets.

您将看到的人,建议您在进入循环,因为这意味着所有的为每一个迭代的方法之前保存的数据结构的大小。

You'll see people recommending that you save a data structure size before entering the loop because it means a method all for each and every iteration.

但是,这是那种微优化的很少事项。不要担心太多关于这种事情,直到你有,告诉你它是一个性能问题的原因的数据。

But this is the kind of micro-optimization that seldom matters. Don't worry much about this kind of thing until you have data that tells you it's the reason for a performance issue.

您应该花更多的时间思考你在一个循环中嵌入算法,并行可能,等那将是远在你的追求更有意义的一个优化的解决方案。

You should be spending more time thinking about the algorithms you're embedding in that loop, possible parallelism, etc. That'll be far more meaningful in your quest for an optimized solution.

这篇关于是否有呼叫。长度数组与保存大小可变的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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