Giter VIP home page Giter VIP logo

Comments (5)

phishman3579 avatar phishman3579 commented on May 10, 2024

Can you share your testing code?

from java-algorithms-implementation.

phishman3579 avatar phishman3579 commented on May 10, 2024

@jolkdarr Thanks again. I've added exception handling to all the fibonaaci calculations. I like your timing code, if you'd like to share it then I'll add it to the repo.

faa1ef5

from java-algorithms-implementation.

jolkdarr avatar jolkdarr commented on May 10, 2024

I have added, in the Sequences class, the following test method to compare all Fibonacci algorithms:

@Test
public void fibonacciBenchmarks() {
    // display benchmarks (output format: markdown):
    System.out.println("# Fibonacci sequence tests");
    final long time_out = 30000;
    final float k = 1000f;
    final Fibonacci[] instances = { FibonacciSequenceUsingLoop.INSTANCE,
                                    FibonacciSequenceUsingRecursion.INSTANCE,
                                    FibonacciSequenceUsingMatrixMultiplication.INSTANCE,
                                    FibonacciSequenceUsingBinetsFormula.INSTANCE };
    for (final Fibonacci f : instances) {
        System.out.println("\n### algorithm: **" + f.getClass().getSimpleName() + "**\n```");
        for (int n = 25; n < 96; n++)
            if (n < 28 || n >= 90 || f == FibonacciSequenceUsingRecursion.INSTANCE)
                try {
                    if (n == 90)
                        System.out.println("...");
                    System.out.print("f(" + n + ") = ");
                    final long t0 = System.nanoTime();
                    final long r = f.fibonacciSequence(n);
                    final float dt = (System.nanoTime() - t0) / k;
                    if (dt <= time_out) {
                        if (dt < k)
                            System.out.print(String.format("%20d [%9.3f µs]", r, dt));
                        else
                            System.out.print(String.format("%20d [%9.3f ms]", r, dt / k));
                        if (n <= 92)
                            System.out.println();
                        else
                            System.out.println(" KO");
                    }
                    else {
                        System.out.println(String.format("%20d %12s", r, "time out"));
                        break;
                    }
                }
                catch (final Throwable t) {
                    System.out.println("?");
                    break; // try next algorithm
                }
        System.out.println("```");
    }
}

Notes:

  1. This snippet is not directly compliant with your repository but it may be adapted easily.
    I made some refactoring in my fork by using the Strategy design pattern. I can provide the modified files.
  2. The output format is markdown. Maybe results ought to be printed in a file (fibonacci_benchmarks.md for instance).

from java-algorithms-implementation.

phishman3579 avatar phishman3579 commented on May 10, 2024

Thanks @jolkdarr , I will try and include this into the timing package. By the way, if you pull the latest version of fibonacciSequenceUsingRecursion it should be significantly faster. I've added memoization to the function.

from java-algorithms-implementation.

jolkdarr avatar jolkdarr commented on May 10, 2024

Ok, I need to rebase quickly then.
Test method (using reflection) adapted for you :)

@Test
public void fibonacciBenchmarks_ref() throws NoSuchMethodException, SecurityException {
    // display benchmarks (output format: markdown):
    System.out.println("# Fibonacci sequence tests");
    final long time_out = 30000;
    final float k = 1000f;
    final Method[] instances = FibonacciSequence.class.getDeclaredMethods();
    for (final Method f : instances) {
        System.out.println("\n### algorithm: **" + f.getName() + "**\n```");
        for (int n = 25; n < 96; n++)
            if (n < 28 || n >= 90 || "fibonacciSequenceUsingRecursion".equals(f.getName()))
                try {
                    if (n == 90)
                        System.out.println("...");
                    System.out.print("f(" + n + ") = ");
                    final long t0 = System.nanoTime();
                    final long r =(Long) f.invoke(null, n);
                    final float dt = (System.nanoTime() - t0) / k;
                    if (dt <= time_out) {
                        if (dt < k)
                            System.out.print(String.format("%20d [%9.3f µs]", r, dt));
                        else
                            System.out.print(String.format("%20d [%9.3f ms]", r, dt / k));
                        if (n <= 92)
                            System.out.println();
                        else
                            System.out.println(" KO");
                    }
                    else {
                        System.out.println(String.format("%20d %12s", r, "time out"));
                        break;
                    }
                }
                catch (final Throwable t) {
                    System.out.println("?");
                    break; // try next algorithm
                }
        System.out.println("```");
    }
}

from java-algorithms-implementation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.