gpt4 book ai didi

java - nanoTime() 在执行时间较长时显示的差异太小

转载 作者:行者123 更新时间:2023-11-29 07:23:37 26 4
gpt4 key购买 nike

<分区>

我正在尝试收集有关执行时间的数据,并比较堆树与 bst 的入队和出队时间。对于流逝的时间,我使用 nanoTime()。

在将未排序和已排序的堆树入队/出队时,随着时间流逝,一切看起来都很好。同样在排队 bst 时。当我尝试将包含 700000 个元素的已排序 bst 出列时,执行需要很长时间。没关系,将排序的 bst 入队时类似。

问题是执行时间不正确。时间应该大约为 15000 毫秒,但我得到的答案大约为 0.5 毫秒(或 459053 ns)。

首先,我将一个包含 640000 个元素(整数)的 bst 排入队列,然后对树进行排序。我感兴趣的是将这个 bst 排队时的执行时间(已经有 640000 个排序的元素)。

这是我的代码(部分):

public static void main(String[] args)
throws DuplicateItemException, FileNotFoundException, IOException, EmptyQueueException {

final String pathFullList = "Files/data_640000.txt";
final String pathPartialList = "Files/data_6400.txt";
final int SIZE_SEVEN = 640000;
final int SIZE_FOR_LIST_TWO = 6400;

BSTPriorityQueue<Integer> bstQueue = new BSTPriorityQueue<Integer>();
bstQueue.clear();

List<Integer> listOne = new ArrayList<Integer>();
listOne = loadList(pathFullList, SIZE_SEVEN);

Collections.sort(listOne);
long execTimeForSort = System.nanoTime() - t2;
System.out.println("Time for sort: " + execTimeForSort);

enqueueBstTree(listOne, bstQueue);
System.out.println("Size of hptree before enqueue: " + hPQueue.size() +
"\n" + "Size of bsttree before enqueue: " + bstQueue.size());

List<Integer> listTwo = new ArrayList<Integer>();
listTwo = loadList(pathPartialList, SIZE_FOR_LIST_TWO);

long t1 = System.nanoTime();
dequeueBstTree(listTwo, bstQueue, SIZE_FOR_LIST_TWO);
long execTime = System.nanoTime() - t1;

System.out.println("Time difference for enqueue/dequeue: " + execTime);
System.out.println("Size of hptree after enqueue/dequeue: " + hPQueue.size() +
"\n" + "Size of bsttree after enqueue/dequeue: " + bstQueue.size());
}

执行此代码可能需要 20 分钟。我从 execTime 得到的答案是 459053 纳秒。

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