减少Java时间和空间复杂度的最佳方法是哪一种? [英] Which is the best way to reduce complexity of time and space in Java?

查看:157
本文介绍了减少Java时间和空间复杂度的最佳方法是哪一种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,并且担心运行时间和空间.

I am writing a program and I am concerned about the time it takes to run and the space it takes.

在程序中,我使用了一个变量来存储数组的长度:

In the program I used a variable to store the length of an array:

int len=newarray3.length;

现在,我想知道是否可以通过不使用len变量来降低空间复杂度,而是在需要时调用newarray3.length;.

Now, I wanted to know that whether I will be able to reduce the space complexity by not using the len variable and instead call the newarray3.length; whenever need.

注意:只有两种情况需要使用长度.

Note: There are only two occasions when the length needs to be used.

推荐答案

我们应该忘记效率低下的问题,比如说大约97%的时间:过早的优化是万恶之源"-唐纳德·努斯(Donald Knuth)

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" - Donald Knuth

首先,单个int变量使用的空间可忽略不计.更一般而言,不必担心这些次要性能(时间和空间")问题.专注于编写清晰易读的程序和高效的算法.如果性能成为问题的根源,那么只要您的通用体系结构是可靠的,您就可以进行优化.因此,请专注于该架构.

First off, a single int variable uses a negligible amount of space. More generally, don't worry about these minor performance ("time and space") issues. Focus on writing clear, readable programs and efficient algorithms. If performance becomes an issue down the line, you'll be able to optimize then as long as your general architecture is solid. So focus on that architecture.

这篇关于减少Java时间和空间复杂度的最佳方法是哪一种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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