Giter Site home page Giter Site logo

Comments (3)

salabim avatar salabim commented on August 26, 2024

I think I have answered this in issue #5

from ycecream.

PFython avatar PFython commented on August 26, 2024

Many thanks for the quick replies and suggestsions Ruud! Very elegant. Using output= catches the results perfectly. In case it's of use here's my final code based on your suggestions:

from statistics import mean, variance
from ycecream import y

def collect(s):
    if not hasattr(y, "results"):
        y.results = {}
    func = s.split("from ")[1].split("()")[0]
    if not y.results.get(func):
        y.results[func] = []
    seconds = float(s.split("in ")[1].split(" seconds")[0])
    y.results[func].append(seconds)

def results(name):
    output = f"\nMean duration for <{name}>:\t"
    output += f"{mean(y.results[name])} seconds"
    output += f"\nMin/Max duration for <{name}>:\t"
    output += f"{min(y.results[name])} to {max(y.results[name])} seconds"
    output += f"\n\nResults for <{name}>:"
    output += f"\n{y.results[name]}"
    return output

benchmark = y.fork(output=collect, show_enter=False)

@benchmark()
def func1():
    for i in range(len(x)):
        s=len(x)

@benchmark()
def func2():
    lx = len(x)
    for i in range(lx):
        s = lx

if __name__ == "__main__":
    x = "x" *6500000
    for func in [func1, func2]:
        for run in range(10):
            func()
        print(results(func.__name__))

It might be beyond the scope of what you intended for ycecream but I do think it's a common scenario for people to want to compare two (or more) different functions and get back a range of duration values while debugging, so if it were possible to internalise something like collect and results into a convenience method in ycecream itself I think it might appeal to an even wider audience, but your call!

from ycecream.

salabim avatar salabim commented on August 26, 2024

I think it is not possible to define a generic function to realise what you would like.
But if you can specify it, I can have a look on how to implement that. For now, I think this issue can be closed.

from ycecream.

Related Issues (9)

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.