当前位置 : 主页 > 编程语言 > java >

Java、Golang、NodeJS、Python冒泡排序速度对比

来源:互联网 收集:自由互联 发布时间:2021-06-28
Python版本 import timedef bubble_sort(unsorted):for i in range(len(unsorted)-1):for j in range(len(unsorted)-i-1):if unsorted[j] unsorted[j+1]:unsorted[j],unsorted[j+1] = unsorted[j+1],unsorted[j]return int(time.time()*1000)startTime = in
Python版本
import time

def bubble_sort(unsorted):
	for i in range(len(unsorted)-1):
		for j in range(len(unsorted)-i-1):
			if unsorted[j] > unsorted[j+1]:
				unsorted[j],unsorted[j+1] = unsorted[j+1],unsorted[j]
	return int(time.time()*1000)

startTime = int(time.time()*1000)
array = []
insideInt = 1000000
for i in range(10000):
	array.append(insideInt-i*3)
stopTime = bubble_sort(array)
print("Use Time = ",stopTime-startTime)
NodeJS版本
var startTime = new Date();
var array = new Array(10000);
var insideInt = 1000000;
for (var i = 0;
     i < array.length;
     i++
) {
    array[i] = insideInt - i * 3;
}
var stopTime = bubble_sort(array);
console.log("Use Time = " + (stopTime.getTime() - startTime.getTime()));

function bubble_sort(unsorted) {
    for (var i = 0; i < unsorted.length; i++) {
        for (var j = i; j < unsorted.length; j++) {
            if (unsorted[i] > unsorted[j]) {
                var temp = unsorted[i];
                unsorted[i] = unsorted[j];
                unsorted[j] = temp;
            }
        }
    }
    return new Date();
}
Golang版本
package main

import (
	"fmt"
	"time"
)

func main() {
	var startTime = time.Now().UnixNano() / 1000000
	var array [10000] int
	insideInt := 1000000
	for i := 0; i < len(array); i++ {
		array[i] = insideInt - i*3
	}
	var stopTime = bubble_sort(array)
	fmt.Print("Use Time = ", stopTime-startTime)
}
func bubble_sort(unsorted [10000] int) int64 {
	for i := 0; i < len(unsorted); i++ {
		for j := i; j < len(unsorted); j++ {
			if unsorted[i] > unsorted[j] {
				var temp = unsorted[i]
				unsorted[i] = unsorted[j]
				unsorted[j] = temp
			}
		}
	}
	return time.Now().UnixNano() / 1000000
}
Java版本
import java.util.Date;

public class run {
    public static void main(String[] args) {
        Date startTime = new Date();
        int[] array = new int[10000];
        int insideInt = 1000000;
        for (int i = 0; i < array.length; i++) {
            array[i] = insideInt - i * 3;
        }
        Date stopTime = bubble_sort(array);
        System.out.println("Use Time = " + (stopTime.getTime() - startTime.getTime()));
    }

    static Date bubble_sort(int[] unsorted) {
        for (int i = 0; i < unsorted.length; i++) {
            for (int j = i; j < unsorted.length; j++) {
                if (unsorted[i] > unsorted[j]) {
                    int temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                    unsorted[j] = temp;
                }
            }
        }
        return new Date();
    }
}
网友评论