gpt4 book ai didi

java - 我的代码运行需要多长时间背后有哪些直觉?

转载 作者:行者123 更新时间:2023-12-01 22:34:02 24 4
gpt4 key购买 nike

我的代码运行需要多长时间背后有哪些直觉? (即在java中将100个对象添加到ArrayList中需要多少时间?)

澄清一下,我不是在寻找计时器。我正在寻找有关每条指令需要多长时间的低级讨论。例如,假设我们有以下设置

Object[] array = new Object[100];
currentIndex = 0;
// I call myList.add(myObject);

每条指令需要多长时间:

指令 1:

数组中的第一个对象指针将引用 myObject?

指令 2:

currentIndex++

指令 3:

if(currentIndex>100) 
// resize internal array

最佳答案

class TimeTest1 {
public static void main(String[] args) {

long startTime = System.currentTimeMillis();

long total = 0;
for (int i = 0; i < 10000000; i++) {
total += i;
}

long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime);
}
}

试试这个

关于java - 我的代码运行需要多长时间背后有哪些直觉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com