Comments (5)
Can you share your testing code?
from java-algorithms-implementation.
@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.
from java-algorithms-implementation.
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:
- 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. - The output format is markdown. Maybe results ought to be printed in a file (
fibonacci_benchmarks.md
for instance).
from java-algorithms-implementation.
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.
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)
- Bug in Queue
- BinaryHeapArray::clear function is broken HOT 3
- There is a 404.
- The Google Group is Invisible
- tip about method BinarySearchTree.replaceNodeWithNode()
- Dsa HOT 1
- Broken KdNode equals and compareTo causes the KdTree not to return all valid results
- Interval Tree: Does it support zero length intervals, and does it support querying all overlapping intervals?
- Bug: MultiplyUsingLoopWithStringInput cant handle zero input
- Refactor: centralize type handling for simple operations such as add and subtract, and add branch coverage for this HOT 1
- The functions Matrix.add and Matrix.subtract have unneeded for loops HOT 1
- [Accidental duplicate issue, please delete me]
- Logical error with American flag sorting
- Report on possible code improvements applying design patterns
- Java data structures
- Possible bug in Prim's algorithm HOT 4
- Bug in finding the "Longest Increasing Subsequence" HOT 2
- Possible bug in ArryaQueue HOT 1
- Possible bug in Graph definition HOT 1
- Radial search for KdTree HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-algorithms-implementation.