Categories > WeAreDevs > Hangout >

Codewars but rewrite my code to make it faster in any language!

Posts: 39

Threads: 8

Joined: Jun, 2023

Reputation: 0

Posted

do what ze title says
rules.
your not allowed to change the main function of the code you can only try to remake or optimise it to be faster

using System;
using System.Collections.Generic;
using System.Diagnostics;

public class FibonacciSequence
{
    private static Dictionary<int, int> fibCache = new Dictionary<int, int>();

    public static void Main(string[] args)
    {
        long totalTime = 0;
        int iterations = 100;
        for (int i = 0; i < iterations; i++)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int e = 0; e < 10000; e++)
            {
                int fibonacciNumber = Fibonacci(e);
                Console.Write(fibonacciNumber + " ");
            }
            stopwatch.Stop();

            totalTime += stopwatch.ElapsedMilliseconds;
        }
        double averageTime = (double)totalTime / iterations;

        Console.WriteLine("\n \n Average time: " + averageTime + " ms");
        Console.WriteLine("Total time " + totalTime / 1000 + " Seconds");

        Console.ReadLine();
    }

    public static int Fibonacci(int n)
    {
        if (fibCache.ContainsKey(n))
        {
            return fibCache[n];
        }

        int fibonacciNumber;
        if (n <= 1)
        {
            fibonacciNumber = n;
        }
        else
        {
            fibonacciNumber = Fibonacci(n - 1) + Fibonacci(n - 2);
        }

        fibCache[n] = fibonacciNumber;
        return fibonacciNumber;
    }
}
  • 0

Added

https://media.discordapp.net/attachments/980907747031789638/1121158601600409720/image.png

  • 0

https://media.discordapp.net/attachments/1074297826219139153/1118491534581567568/Mask_group.png

Posts: 1

Threads: 0

Joined: Jun, 2023

Reputation: 0

Replied

rus is the future and the future is now

https://cdn.discordapp.com/attachments/1068626873996808394/1121167612521492490/image.png

use std::collections::HashMap;
use std::time::{Instant};

fn main() {
    let mut fib_cache: HashMap<u64, u128> = HashMap::new();

    let iterations = 100;
    let start_time = Instant::now();
    for _ in 0..iterations {
        let mut fibonacci_numbers = Vec::new();


        for e in 0..10_000 {
            let fibonacci_number = fibonacci(e, &mut fib_cache);
            fibonacci_numbers.push(fibonacci_number.to_string());
        }


        println!("{}", fibonacci_numbers.join(" "));
    }
    let elapsed_time = start_time.elapsed();
    let average_time = elapsed_time.as_millis() / iterations as u128;

    println!("\nAverage time: {:?} ms", average_time);
    println!("Total time: {:?} seconds", elapsed_time.as_secs());
}

fn fibonacci(n: u64, fib_cache: &mut HashMap<u64, u128>) -> u128 {
    if let Some(&fibonacci_number) = fib_cache.get(&n) {
        return fibonacci_number;
    }

    let fibonacci_number: u128;
    if n <= 1 {
        fibonacci_number = n as u128;
    } else {
        fibonacci_number = fibonacci(n - 1, fib_cache) + fibonacci(n - 2, fib_cache);
    }

    fib_cache.insert(n, fibonacci_number);
    fibonacci_number
}
  • 0

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )