gpt4 book ai didi

Java 计算平均执行时间

转载 作者:行者123 更新时间:2023-12-01 06:47:04 25 4
gpt4 key购买 nike

我想计算 x 次运行的平均执行时间(即 10)...我可以使用 main 方法中的循环轻松执行 10 次,但如何存储执行时间并计算平均值?我认为这非常简单,但我现在一片空白......提前致谢!

import java.util.Arrays;
import java.util.Random;

public class OptQSort1 {

static boolean insertionSortCalled = false;
private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 9999;

private static int[] randomArray(int size) {

// Randomize data (array)
final int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(RANDOM_INT_RANGE);
}
return arr;
}

// Sort
private static void sort(int[] arr) {
if (arr.length > 0)
sortInPlace(arr, 0, arr.length - 1);
}

private static void sortInPlace(int[] arr, int left, int right) {



// OptQSort1:
int size = right - left + 1;
if (size < 10 && !insertionSortCalled) {
insertionSortCalled = true;
insertionSort(arr, 0, arr.length - 1);
}

if (left >= right)
return; // sorted

final int range = right - left + 1;
int pivot = random.nextInt(range) + left;

int newPivot = partition(arr, left, right, pivot);

sortInPlace(arr, left, newPivot - 1);
sortInPlace(arr, newPivot + 1, right);

}

private static int partition(int[] arr, int left, int right, int pivot) {

int pivotVal = arr[pivot];
swapArrayVals(arr, pivot, right);

int storeIndex = left;
for (int i = left; i <= (right - 1); i++) {
if (arr[i] < pivotVal) {
swapArrayVals(arr, i, storeIndex);
storeIndex++;
}
}

swapArrayVals(arr, storeIndex, right);

return storeIndex;
}

private static void swapArrayVals(int[] arr, int from, int to) {
int fromVal = arr[from];
int toVal = arr[to];
arr[from] = toVal;
arr[to] = fromVal;
}

public static void insertionSort(int[] arr, int left, int right) {
int in, out;

for (out = left + 1; out <= right; out++) {
int temp = arr[out];
in = out;

while (in > left && arr[in - 1] >= temp) {
arr[in] = arr[in - 1];
--in;
}
arr[in] = temp;
}

}

public static void main(String[] args) {

long StartTime = System.nanoTime();

int runCount = 0;

// Array size
int[] arr = randomArray(1000);
int[] copy = Arrays.copyOf(arr, arr.length);

// Print original data (array)
System.out.println("The starting/unsorted array: \n"
+ Arrays.toString(arr));

sort(arr);

do {
// check the result
Arrays.sort(copy);
if (Arrays.equals(arr, copy)) {
System.out.println("The ending/sorted array: \n"
+ Arrays.toString(arr));


// print time
long TotalTime = System.nanoTime() - StartTime;
System.out.println("Total elapsed time (milliseconds) " + "is: "
+ TotalTime + "\n");

runCount++;
}
} while (runCount < 10);
}

}

最佳答案

您可以通过测量代码 10 次迭代的总时间,然后将其除以 10 来计算平均值。

例如:

public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 10; ++i) {
doSort();
}
long elapsed = System.currentTimeMillis() - start;
long average = elapsed / 10;
}

作为一个有用的提示,请使用命名常量而不是文字值来表示迭代次数:

private final static int ITERATIONS = 10;
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; ++i) {
doSort();
}
long elapsed = System.currentTimeMillis() - start;
long average = elapsed / ITERATIONS;
}

这意味着如果您想运行 50 或 100 次迭代,则只需更改一处的数字。

您还应该意识到,从此类实验中获得准确的计时结果是非常困难的。最好包含一个“预热”阶段,以允许 JIT 评估和优化代码,并进行更多的迭代:

private static final int WARMUP_ITERATIONS = 10000;
private static final int RUN_ITERATIONS = 100000;
public static void main(String[] args) {
// Warmup with no timing
for (int i = 0; i < WARMUP_ITERATIONS; ++i) {
doSort();
}

// Now the real test
long start = System.currentTimeMillis();
for (int i = 0; i < RUN_ITERATIONS; ++i) {
doSort();
}
long elapsed = System.currentTimeMillis() - start;
long average = elapsed / RUN_ITERATIONS;
}

关于Java 计算平均执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378453/

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