Code Monkey home page Code Monkey logo

my-py-sort-test's Introduction

Self-contained Python program for benchmarking sorting algorithms

Usage

python sorttest.py SORTALG NUM SEED [CHECK]
    SORTALG - Sorting algorithm: bubble, selection, merge, quick
        NUM - Number of elements to sort
       SEED - Seed for random number generator
      CHECK - Check if sorting is correct

For example, invoking sorttest to sort an integer array of 100,000 elements using Bubble sort and a random number generator seed of 99823:

$ python sorttest.py bubble 100000 99823

If a 4th argument is given, the program checks the correctness of the sorting procedure:

$ python sorttest.py bubble 100000 34545 yes
Sorting Ok!

Benchmarking with GNU Time

In several Linux OSes the GNU Time utility must be explicitly installed using the package manager and invoked as /usr/bin/time. On OSX it should be installed using Homebrew or similar, and it is invoked as gtime.

It is also possible to use the shell built-in time command, which is available by default on Linux and OSX. For Windows, Cygwin and MinGW provide a Bash shell with this command, but there are native alternatives.

Default output format

The PerfAndPubTools benchmark analysis functions accept the default output format of the GNU Time command, as shown in the following example:

$ /usr/bin/time python sorttest.py merge 500000 99823
3.22user 0.01system 0:03.23elapsed 100%CPU (0avgtext+0avgdata 48672maxresident)k
0inputs+0outputs (0major+12088minor)pagefaults 0swaps

Of course, the output should be redirected to a file in order to be used by PerfAndPubTools:

$ /usr/bin/time python sorttest.py quick 1000000 2362 2> time.txt 
Alternative output formats

The -f option allows to format the output of the GNU Time command. For example, the %e format specifier shows the elapsed wall clock time used by the process (in seconds), while the %M specifier displays the maximum resident set size of the process during its lifetime, in Kilobytes. These are useful for testing the time and space complexity of each algorithm:

$ /usr/bin/time -f "%e sec.\n%M Kb\n" python sorttest.py bubble 100000 128
607.27 sec.
18408 Kb

$ /usr/bin/time -f "%e sec.\n%M Kb\n" python sorttest.py selection 100000 128
331.03 sec.
18856 Kb

$ /usr/bin/time -f "%e sec.\n%M Kb\n" python sorttest.py merge 100000 128
0.58 sec.
16948 Kb

$ /usr/bin/time -f "%e sec.\n%M Kb\n" python sorttest.py quick 100000 128
0.48 sec.
17504 Kb

License

MIT License

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.