Git Product home page Git Product logo

algorithmic-complexity's Introduction

Algorithmic Complexity

The aim is to understand efficiency as one of the dimensions of a good piece of code, using a timing framework to benchmark algorithms.

The framework,

  • Returns the time needed to execute a function on an array.
  • The framework runs the function multiple times, with the array size increasing each time.
  • An example input array could increase in length from 50,000 to 1,000,000 in steps of 50,000.
  • Each run outputs the time to the console.

Getting Started

> git clone [email protected]:shektor/algorithmic-complexity.git
> cd algorithmic-complexity
> npm install

Running Tests

Unit tests are executed with Jest, including code coverage, with linting performed post-test by ESLint.

> npm test

Usage

Algorithms can be written as functions and saved as seperate files in the algorithms folder. The function should accept an array as an arguement and return a modified array. The function should be exported as a module at the end of file.

// ./src/algorithms

function myAlgorithm (array) {
  // modify array
  return array
}

module.exports = myAlgorithm

Algorithms can be timed using benchmarkRunner.js by requiring the module, and passing it to the Benchmark.test function.

const myAlgorithm = require('./src/algorithms/myAlgorithm')

const benchmark = new Benchmark(50000, 1000000)

console.log(benchmark.test(myAlgorithm))

The runner can be executed in the terminal using node, with output returned to the console.

โžœ node benchmarkRunner.js
[ [ 50000, 4 ],
  [ 100000, 4 ],
  [ 150000, 3 ],
  [ 200000, 7 ],
  [ 250000, 3 ],
  [ 300000, 9 ],
  [ 350000, 9 ],
  [ 400000, 12 ],
  [ 450000, 10 ],
  [ 500000, 12 ],
  [ 550000, 14 ],
  [ 600000, 16 ],
  [ 650000, 15 ],
  [ 700000, 14 ],
  [ 750000, 15 ],
  [ 800000, 14 ],
  [ 850000, 15 ],
  [ 900000, 21 ],
  [ 950000, 22 ],
  [ 1000000, 21 ] ]

The test function returns an array, with each element being an array where the first value is the length of the randomised array passed to the algorithm, and the second value the duration in milliseconds.

Benchmark Documentation

Can be used to time the duration it takes to run an algorithm against a randomised array that increases in length at specified steps.

Syntax

new Benchmark([lengthIncrease, maxLength])

Parameters

lengthIncrease Optional

  • Integer value representing the increase in length of the array passed to the algorithm being tested after each run. The default is 50000.

maxLength Optional

  • Integer value representing the max array length the algorithm will be tested against. The default is 1000000.

Methods

.test(function)

  • Returns an array with each element composed of an array containing milliseconds taken to run pass function against array length. The method accepts a function that is executed inside the method, with an array passed as an arguement to the function.

algorithmic-complexity's People

Contributors

shektor avatar dependabot[bot] avatar

Watchers

James Cloos 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.