栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

算法,比较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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号