天天看點

c java python go_對比JAVA、Python、C、Go運作時間,我驚呆了!!!

對比JAVA、Python、C、Go運作時間,我驚呆了!!!

周末在寝室刷完算法,想放松一下,于是做了一個實驗:用現在主流的幾種程式設計語言對0 - (10000000 - 1)求和,結果我驚呆了,話不多說,show me your code!!!

python

import time

sum = 0

start = time.time()

for i in range(10000000):

sum += i

end = time.time()

print(end - start)

java

public static void main(String[] args) {

long startTime=System.currentTimeMillis(); //擷取開始時間

int sum = 0;

for(int i=0;i<10000000;i++){

sum +=i;

}

long endTime=System.currentTimeMillis(); //擷取結束時間

System.out.println(endTime - startTime);

}

Go

package main

import "fmt"

import "time"

func main(){

sum := 0

var start = time.Now()

for i := 0; i< 10000000; i++ {

sum += i

}

fmt.Println(sum)

var end = time.Since(start)

fmt.Println(end)

}

C

#include

#include

int main() {

clock_t start, end;

double duration, sum;

start = clock();

for (int i = 0; i < 10000000; i++){

sum += i;

}

end = clock();

duration = (double)(end - start) / CLOCKS_PER_SEC;

printf("%f s", duration);

return 0;

}

我的電腦是magicbook,8G記憶體,運作的各自結果如下:

go 7.9944ms

c 0.036s

python 1.96s

java 16ms

關于python的結果早就料想到了,但另外3中語言居然超出了預料!!!