Comments (3)
I think I have answered this in issue #5
from ycecream.
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.
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)
- TypeError: 'NoneType' object is not subscriptable HOT 6
- Make ycecream work from REPL HOT 1
- Enhancement: Add support for REPL sessions HOT 2
- as_str=True not returning same output HOT 3
- Decorator args can't be over-written? HOT 4
- Feature request: Hard line length limit HOT 3
- ycecream printing in ASCII instead of Unicode? HOT 1
- Coloring option HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ycecream.