这段代码可以更高效吗?
这个程序应该这样做
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
所以这是我的代码:
public class ex_4_21 {
public static void main( String Args[] ){
int process = 1;
int process2 = 1;
int process22 = 1;
int process3 = 1;
int process33 = 2;
System.out.println("N 10*N 100*N 1000*N");
while(process<=5){
while(process2<=3){
System.out.printf("%d ",process2);
while(process22<=3){
process2 = process2 * 10;
System.out.printf("%d ",process2);
process22++;
}
process2++;
}
process++;
}
}
}
我的代码可以更有效吗?我目前正在学习 while 循环。到目前为止,这是我得到的。任何人都可以提高效率,或者给我关于如何提高代码效率的想法吗?
这不是作业,我是自学java
This Program should do this
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
So here's my code:
public class ex_4_21 {
public static void main( String Args[] ){
int process = 1;
int process2 = 1;
int process22 = 1;
int process3 = 1;
int process33 = 2;
System.out.println("N 10*N 100*N 1000*N");
while(process<=5){
while(process2<=3){
System.out.printf("%d ",process2);
while(process22<=3){
process2 = process2 * 10;
System.out.printf("%d ",process2);
process22++;
}
process2++;
}
process++;
}
}
}
Can my code be more effecient? I am currently learning while loops. So far this what I got. Can anyone make this more efficient, or give me ideas on how to make my code more efficient?
This is not a homework, i am self studying java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用单个变量 n 来执行此操作。
如果 10 的幂是可变的那么你可以尝试这个:
You can use a single variable n to do this.
if the power of 10 is variable then you can try this:
如果必须使用 while 循环
If you must use a while loop
您的 while 循环过多(您的“process2”while 循环是不必要的)。您似乎还存在一些与以下事实相关的错误:您在内部循环中循环的变量不会在每次迭代时重新初始化。
我还建议不要使用 while 循环;你的例子更适合 for 循环;我知道您正在尝试学习循环机制,但学习的一部分还应该是决定何时使用哪种结构。这实际上不是性能建议,更多的是方法建议。
对于您正在尝试做的事情,我没有任何进一步的性能改进建议;显然,您可以删除循环(下降到单个循环甚至没有循环),但是两个循环对于您正在做的事情是有意义的(允许您轻松地以最小的更改向输出添加另一行或列)。
You have one too many while loops (your "process2" while loop is unnecessary). You also appear to have some bugs related to the fact that the variables you are looping on in the inner loops are not re-initialized with each iteration.
I would also recommend against while loops for this; Your example fits a for loop much better; I understand you are trying to learn the looping mechanism, but part of learning should also be in deciding when to use which construct. This really isn't a performance recommendation, more an approach recommendation.
I don't have any further performance improvement suggestions, for what you are trying to do; You could obviously remove loops (dropping down to a single or even no loops), but two loops makes sense for what you are doing (allows you to easily add another row or column to the output with minimal changes).
您可以尝试循环展开,类似于@Vincent Ramdhanie 的答案。
然而,对于如此小的样本,循环展开和线程化不会产生显着的性能改进。创建和启动线程(进程)所涉及的开销比简单的 while 循环花费更多的时间。 I/O 开销将比展开版本节省的时间更多。复杂的程序比简单的程序更难调试和维护。
您认为这称为微优化。仅当无法满足要求或客户有要求时,才保存对大型程序的优化。
You can try loop unrolling, similar to @Vincent Ramdhanie's answer.
However, loop unrolling and threading won't produce a significant performance improvement for such a small sample. The overhead involved in creating and launching threads (processes) takes more time than a simple
while
loop. The overhead in I/O will take more time than the unrolled version saves. A complex program is harder to debug and maintain than a simple one.You're thinking is called microoptimization. Save the optimizations for larger programs and only when the requirements cannot be met or the customer(s) demand so.