先来个开胃菜,java冒泡排序,随便来一千万次,数组有12个元素,该对比考验cpu单核性能
:
r5 2600:
i7 9750h:
代码如下:
下面方便复制
:
public static void main(String[] args) { long nn = System.nanoTime(); // 冒泡排序算法 int[] numbers = new int[] { 1, 5, 8, 2, 3, 9, 4,11,54,62,10,344 }; int i, j, times = 10000000; for (int t = 0; t < times; t++) { for (i = 0; i < numbers.length - 1; i++) { for (j = 0; j < numbers.length - 1 - i; j++) { if (numbers[j] > numbers[j + 1]) { int temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } } nn = System.nanoTime() - nn;// 单位为ns(纳秒,即10的负9次方秒), System.out.println((nn / 1000000) + " ms"); }