Code Monkey home page Code Monkey logo

pythonbenchmark's Introduction

Learn to write faster code by experimenting with this library

pip install pythonbenchmark

  • The timeit module that comes with python is only useful for small bits of Python code not for functions.

  • This library solves that. It provides an intuitive way to measure the execution time of functions and compare the relative speed of two functions.

  • Optimizing your code? Curious how much speed you gained? No problem

  • Pythonbenchmark allows this by letting you compare two functions which take the same input and measure which one gets the job done faster.

compare(myFunction, myOptimizedFunction, 10)
  • Additionally you can just put a decorator on the functions, pythonbenchmark will measure them and print out the execution time in the console.
@measure
def myFunction(something):
    [x*x for x in range(1000000)]

How-To:

A typical use case is: I have functionX, and optimized functionX. Now I want to know if my modified version is faster and how much.

from pythonbenchmark import compare, measure
import time

a,b,c,d,e = 10,10,10,10,10
something = [a,b,c,d,e]

def myFunction(something):
	time.sleep(0.4)

def myOptimizedFunction(something):
	time.sleep(0.2)

# comparing test
compare(myFunction, myOptimizedFunction, 10, input)
# without input
compare(myFunction, myOptimizedFunction, 100)

Output

alt tag

Measuring execution time with the @measure decorator

from pythonbenchmark import compare, measure
import time

a,b,c,d,e = 10,10,10,10,10
something = [a,b,c,d,e]

@measure
def myFunction(something):
	time.sleep(0.4)

@measure
def myOptimizedFunction(something):
	time.sleep(0.2)

myFunction(input)
myOptimizedFunction(input)

Output

alt tag


Do experiments, have fun!
Make incremental changes to your high performance code and see how much speed improvement you gain with each modification. This will give you an enormous intuition about how to write fast code :)

A spanish article about how I came up with this library is on my Blog.

pythonbenchmark's People

Contributors

karlheinzniebuhr avatar bitdeli-chef avatar bryant1410 avatar

Watchers

 avatar  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.