运行环境:macOS m1
javascript
function fid(n) {
if (n == 0) return 0
if (n == 1) return 1
return fid(n - 1) + fid(n - 2)
}
let start_time = Date.now();
fid(50)
let end_time = Date.now();
console.log(`一共花费的时间为:${(end_time - start_time)/1000}s`); // 一共花费的时间为:170.906s
console.log('结束!');
dotnet6 c#10
var t1 = DateTime.Now;
fid(50);
var t2 = DateTime.Now;
Console.WriteLine(t2-t1); // 00:01:14.6404730 -> 74s
long fid(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fid(n-1) + fid(n-2);
}
java
import java.util.*;
class Test {
public static void main(String[] args){
long t1 = new Date().getTime();
long num = fid(50);
System.out.println(num); // 12586269025
long t2 = new Date().getTime();
System.out.printf("time use %d ms", t2-t1); //time use 39455 ms% == 39s
}
public static long fid(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fid(n-1) + fid(n-2);
}
}
go
package main
import (
"fmt"
"time"
)
func main() {
t1 := time.Now()
num := fid(50)
fmt.Println(num)
useTime := time.Since(t1) // 12586269025 58s
fmt.Println(useTime)
}
func fid(n uint64) uint64 {
if n == 0 {
return 0
}
if n == 1 {
return 1
}
return fid(n-1) + fid(n-2)
}
c++
#include "iostream"
#include "time.h"
long int fid(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fid(n - 1) + fid(n - 2);
}
int main()
{
time_t t1 = time(NULL);
long int num=fid(50);
std::cout << num << std::endl;
time_t t2 = time(NULL);
std::cout << "use time: " << t2-t1 << std::endl; // 108s
return 0;
}
rust
use std::time::Instant;
fn main() {
let start=Instant::now();
let num = fid(50);
println!("{}", num);
println!("time use {:?}", start.elapsed()); // 12586269025 time use 34.561594s
}
fn fid(num: u64) -> u64 {
match num {
0 => 0,
1 => 1,
_ => fid(num - 1) + fid(num - 2)
}
}
以上结论:
macOS-M1运行环境下:
- javascript- 大约170s
- c++- 大约108s
- c#- 大约74s
- golang- 大约58s
- java17- 大约39s
- rust- 大约34s
Windows运行环境下:
- javascript- 大约155.7s
- c++- 大约91s
- c#- 大约73s
- golang- 大约61s
- java17- 大约53.5s
- rust- 大约29.8s