[英]Why does my Java drawing code take so much CPU?
I learned Java for 3 days and made a code to draw a diagonal line made from X. It's taking 28% of my i5 CPU and I don't know why. 我花了3天的时间学习Java,并编写了代码以绘制用X绘制的对角线。这占用了我的i5 CPU的28%,我不知道为什么。
public class Main {
public static void main(String[] args) {
for (int i=0; i<20000; i++) {
for (int j=0; j<i; j++) {
System.out.print(" ");
}
System.out.println("X");
}
}
}
Edit 1: By the way, the output is kinda different between the first minutes I ran it and 10 minutes later. 编辑1:顺便说一下,在我运行它的前几分钟和10分钟之后,输出有点不同。 Weird.
奇怪的。 The distance between X get longer.
X之间的距离变长。 Screenshot in 10 minutes later
10分钟后截图
Edit 2: I made another code just to know the "progress" where i has reached, when i reaches 100, 200, etc. it'll print that. 编辑2:我编写了另一个代码,只是想知道到达的“进度”,当我达到100、200等时,它将打印出来。 But the code failed to compile on Windows, it compiles fine on https://www.compilejava.net .
但是代码无法在Windows上编译,可以在https://www.compilejava.net上正常编译。 What's the problem?
有什么问题?
public class Main {
public static void main(String[] args) {
for (int i=0; i<20000; i++) {
for (int j=0; j<i; j++)
{
System.out.print(" ");
}
System.out.println("X");
if ((i % 100)==0) {
System.out.println("Your cute code made it to the 100th lapse!");
}
}
}
}
To demonstrate what is happening here, consider the following code: 为了演示这里发生的事情,请考虑以下代码:
public static void main(String[] args) {
int iTotal = 0;
int jTotal = 0;
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < i; j++) {
jTotal += 1;
}
iTotal += 1;
}
System.out.println("Total I: " + iTotal);
System.out.println("Total J: " + jTotal);
System.out.println("Total: " + jTotal + iTotal);
}
Here I've removed the printing and just used counters to help you see exactly what is happening here 在这里,我删除了打印件,仅使用计数器来帮助您准确了解此处发生的情况
The output: 输出:
Total I: 20,000
Total J: 199,990,000
Total: 19,999,000,020,000
Well, it is quite obvious that it will take a lot! 好吧,很明显这将花费很多! it will iterate a lot of times!
它将迭代很多次! every value of j will be the summation of 20000 (1+2+3+4+...+19999+20000).
j的每个值都是20000(1 + 2 + 3 + 4 + ... + 19999 + 20000)的总和。
So the complexity is higher than O(n)
. 因此,复杂度高于
O(n)
。
Taking that into account, the number of prints you are doing is REALLY high. 考虑到这一点,您正在执行的打印数量确实很高。 20000 times X and summation of 20000 a white space!
20000倍X和20000空格的总和!
Your code attempts to print 20000 'X' characters, as was observed in comments, but much worse, it attempts to print a total of about 400000000 space characters. 正如注释中所观察到的那样,您的代码尝试打印20000个 “ X”字符,但更糟糕的是,它尝试打印总共约4亿个空格字符。 Computers are fast, but that will still take some effort.
电脑速度很快,但这仍然需要一些努力。
You're executing about 200 million print calls, if my high-school math is correctly remembered. 如果正确记住我的高中数学,则您将执行大约2亿个打印呼叫。 You're producing 20 thousand lines of screen output.
您正在产生2万行屏幕输出。 It doesn't seem surprising that uses a reasonably significant amount of CPU and takes some time to complete.
使用相当大量的CPU并花费一些时间来完成似乎并不奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.