目的
1. 用语言自带的时间库测量程序运行的时间
2. 比较相同基础代码(以冒泡排序为例)下语言的运行速度
cpp (gcc 10.2)
// 核心部分
auto start = system_clock::now();
//...代码部分
auto finish = system_clock::now();
auto duration = duration_cast<microseconds>(finish - start);
auto cost = double(duration.count()) * microseconds::period::num / microseconds::period::den;
// ===========================
// 完整代码
#include "iostream"
#include "chrono"
#include "vector"
using namespace std;
using namespace chrono;
void bubbleSort(vector<int> &a) {
int len_a = (int) a.size();
for (int j = 0; j < len_a - 1; ++j) {
for (int i = 0; i < len_a - 1 - j; ++i) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
int main() {
auto arr = vector<int>{};
for (int i = 0; i < 3000; ++i) {
arr.push_back(10 - i);
}
auto start = system_clock::now();
bubbleSort(arr);
auto finish = system_clock::now();
auto duration = duration_cast<microseconds>(finish - start);
auto cost = double(duration.count()) * microseconds::period::num / microseconds::period::den;
for (const auto &item : arr) {
cout << item << "\n";
}
cout << "cost: " << cost << "ms" << endl;
}
java (jdk15)
// 核心部分
long start = System.nanoTime();
//...代码部分
long finish = System.nanoTime();
double cost = (double) (finish - start) / 1000000;
// ===========================
// 完整代码
public class Test {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < 3000; i++) {
arr.add(10 - i);
}
long start = System.nanoTime();
bubbleSort(arr);
long finish = System.nanoTime();
double cost = (double) (finish - start) / 1000000;
for (Integer integer : arr) {
System.out.println(integer);
}
System.out.println("cost: " + cost + "ms");
}
public static void bubbleSort(ArrayList<Integer> a) {
int lenA = a.size();
int t1, t2;
for (int j = 0; j < lenA - 1; j++) {
for (int i = 0; i < lenA - 1 - j; i++) {
if ((t1 = a.get(i)) > (t2 = a.get(i + 1))) {
a.set(i, t2);
a.set(i + 1, t1);
}
}
}
}
}
python (python 3.9.2)
# 核心部分
start = time.time()
# ...代码部分
finish = time.time()
cost = (finish - start) * 1000
# ===========================
# 完整代码
import time
def bubbleSort(a: list):
len_a = len(a)
for j in range(len_a - 1):
for i in range(len_a - 1 - j):
if a[i] > a[i + 1]:
a[i], a[i + 1] = a[i + 1], a[i]
def main():
arr = [10 - x for x in range(3000)]
start = time.time()
bubbleSort(arr)
finish = time.time()
for el in arr:
print(el)
cost = (finish - start) * 1000
print("cost: ", cost, "ms", sep='')
if __name__ == '__main__':
main()
go (1.16.4)
// 核心部分
start = time.time()
//...代码部分
finish = time.time()
cost = (finish - start) * 1000
// ===========================
// 完整代码
package main
import (
"fmt"
"time"
)
func main() {
var arr []int
for i := 0; i < 3000; i++ {
arr = append(arr, 10-i)
}
start := time.Now()
bubbleSort(&arr)
cost := time.Since(start)
defer fmt.Println("cost:", cost)
for _, el := range arr {
fmt.Println(el)
}
}
func bubbleSort(a *[]int) {
arr, lenA := *a, len(*a)
for j := 0; j < lenA-1; j++ {
for i := 0; i < lenA-1-j; i++ {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
}
}
}
}
运行时间显示
cpp
java
python
go
结论
显然,代码运行时间
c++ < go < java < python
c++ 在 0.01ms数量级上
go 在1ms数量级上
java和python 在100ms数量级上
但java比python已有明显的差距(快8倍)
注意
本次测试仅在3000个int数据中测试,在冒泡排序最坏情况下运行,若数据数量级加大,时间消耗将会更加明显。
本次实验为不严谨实验,存在偶然误差,所以粗略比较大小,感受各语言在运行速度上的差距。