Coder Social home page Coder Social logo

iso53 / java-algorithm-speed-comparison Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 31 KB

A Java project that compares algorithm speeds, executing tests and visualizing results.

License: GNU General Public License v3.0

Java 100.00%
algorithm java speed-comparison

java-algorithm-speed-comparison's Introduction

Java Algorithm Speed Comparison

This Java project aims to compare algorithm speeds by running tests and visualizing the results using a line chart. The project consists of several classes that work together to facilitate the comparison process.

Table of Contents

Classes

  • Method: The Method class represents a method or algorithm to be tested. It includes an interface IMethod and allows the execution of a method with parameters.
  • TestCase: The TestCase class facilitates the creation of test cases with varying parameters. It is designed using the builder pattern.
  • TestGenerator: The TestGenerator class orchestrates the testing process, running specified methods with different test cases and collecting execution times. It also provides a method to visualize the results using a line chart.
  • LineChart: The LineChart class utilizes JFreeChart to create a line chart for visualizing the execution times of different methods across multiple test runs.

How to Use

Let's say we want to compare two methods that checks whether a string is palindrome or not.

Create Method Instance

Create instances of the Method class to represent the algorithms you want to compare. You can create a method instance like this:

Method method1 = new Method("Method_1", (params) -> {
        String str = (String) params[0];
        (new StringBuilder(str).reverse().toString()).equals(str);
    });

Method method2 = new Method("Method_2", (params) -> {
      String str = (String) params[0];
      int i = 0, j = str.length() - 1;
      while (i < j) {
          if (str.charAt(i) != str.charAt(j)) {
              break;
          }
          i++;
          j--;
      }
  });

Note

Keep in mind that these functions does not return a values as they solely focuses on comparing the speed difference and does not check the correctness of the answer.

Create Parameters

Create parameters to pass to the methods you just create.

int testLength = 1024;
String[] palindromeArr = new String[testLength];
// Rest of the code for creating arrays full of palindrome strings.
// Preferably strings with increasing length.

Create Test Cases

Use the TestCase.Builder to define test cases with specific parameters.

TestCase tc = new TestCase
            .Builder()
            .setParameterCount(1)         // you need to add this first
            .setTestCount(testLength)     // and add this second
            .addParameter(palindromeArr)  // you can add as much parameter as you want
            .build();

Create Test Generator

Utilize the TestGenerator.Builder to set up the testing environment by adding methods and test cases.

TestGenerator tg = new TestGenerator
            .Builder()
            .setTestName("'IsPalindrome' Algorithm Speed Comparison") // optional
            .addMethod(method1) // you must add at least one method
            .addMethod(method2) // you can add as much method as you want
            .addTestCase(tc)    // you must add only one test case.
                                // if you add more than one the last one will be used
            .build();

Run the Test

Invoke the run() method of the TestGenerator to execute the tests.

// run the test
tg.run();

Visualize the Results

Call the visualize() method of the TestGenerator to display a line chart showing the execution times of each method across different test cases.

// visualize the test on a line chart. (thx to jFreeChart)
tg.visualize();

Contributing

Your contributions are valued! We appreciate your cooperation in making our project better.

License

This project is licensed under the GNU General Public License v3.0. Feel free to modify the content and structure based on your preferences and project specifics.

Follow me on GitHub

java-algorithm-speed-comparison's People

Contributors

iso53 avatar

Stargazers

 avatar

Watchers

 avatar

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.