go java nodejs 速度对比_Java、Golang、NodeJS、Python冒泡排序速度对比

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值