0
点赞
收藏
分享

微信扫一扫

算法,比较rust golang nodejs 斐波那契算法


运行环境: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

🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈
❤️🧡💛💚💙💜🤎❤️🧡💛💚💙💜🤎❤️🧡💛💚💙💜🤎❤️🧡💛💚💙💜🤎

现在采用优化版

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

long long fib2(long int n) {
//1.迭代
if(n==0)
{
return 0;
}
if(n==1)
{
return 1;
}
long int first=1;
long int second=1;
long int third=1;
n-=2;
while(n)
{
third=(first+second);
first=second;
second=third;
n--;
}
return third; // 12586269025
}

int main()
{
std::cout << "hello \n";

time_t t1 = time(NULL);

long long num=fib2(50);
std::cout << num << std::endl;

time_t t2 = time(NULL);
std::cout << "use time: " << t2-t1 << std::endl;

return 0;
}

java

import java.util.*;

class Test {
public static void main(String[] args){
long t1 = new Date().getTime();

long num = fid2(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);
}

public static long fid2(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}

long first=1;
long second=1;
long third=1;
n-=2;

while(n != 0){
third=(first+second);
first=second;
second=third;
n--;
}
return third;
}

}

JavaScript

function fid(n) {
if (n == 0) return 0
if (n == 1) return 1
return fid(n - 1) + fid(n - 2)
}

function fid2(n) {
if (n == 0) return 0
if (n == 1) return 1

let first = 1
let second = 1
let third = 1
n -= 2

while (n != 0) {
third = first + second
first = second
second = third
n--
}
return third
}

let start_time = Date.now()

let num = fid2(50)
console.log(num)

let end_time = Date.now()
console.log(`一共花费的时间为:${(end_time - start_time) / 1000}s`) // 一共花费的时间为:0.006s
console.log('结束!')

golang

package main

import (
"fmt"
"time"
)

func main() {
t1 := time.Now()

num := fid2(80)
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)
}

func fid2(n uint64) uint64 {
if n == 0 {
return 0
}
if n == 1 {
return 1
}

first := 1
second := 1
third := 1
n -= 2

for {
third = first + second
first = second
second = third
n--

if n <= 0 {
break
}
}
return uint64(third)
}

结论: 基本都是 0s


举报

相关推荐

0 条评论