New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cProfile behaviour issue with decorator and math.factorial() lib. #83174
Comments
Please, could you write a description to this issue here instead of the attached file? |
Hello, I use decorators in my Python project. I tested project with cProfile(profiling). Then, import functools
import cProfile
def decor(func):
@functools.wraps(func)
def wraps(*args, **kwargs):
return func(*args, **kwargs)
return wraps
@decor
def count(g_val):
if g_val < 1:
print("End")
else:
count(g_val - 1)
cProfile.run('count(102)') OS names: Windows, Linux - x64 # cProfile using into the .py file End Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) # Instead of 102 used ---> 82, 22, 33, 72 and etc. also gave me strange results like in 102. import functools
import cProfile
def decor(func):
@functools.wraps(func)
def wraps(*args, **kwargs):
return func(*args, **kwargs)
return wraps
@decor
def count(g_val):
if g_val < 1:
print("End")
else:
count(g_val - 1)
count(102) # cProfile using via command line End Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) Here also interesting results in computation with some factorial estimations: import math
import cProfile
def fact(n):
return sum(1 / math.factorial(n) for n in range(18)) print(cProfile.run('fact(200)')) # cProfile into the .py file 42 function calls in 0.000 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) import math
import cProfile
def fact(n):
print(sum(1 / math.factorial(n) for n in range(18)))
fact(200) # cProfile using via command line 2.7182818284590455 Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) In cProfile documentation isn't talking about command line behaviour difference. |
@avicenna Can you clarify exactly which part of the output you find surprising, and why, and what result you expected instead? It's a little hard to tell which details in your message we're supposed to be looking at. |
In short, here are different behaviours in increasing steps of values, which are (based on my researching) giving incorrect results in relation to each other. Given example: import functools
import cProfile
def decor(func):
@functools.wraps(func)
def wraps(*args, **kwargs):
return func(*args, **kwargs)
return wraps
@decor
def count(g_val):
if g_val < 1:
print("End")
else:
count(g_val - 1)
cProfile.run('count(VALUE)')
Below I wrote results by given values...
End Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function)
As you see above samples are giving surprise results.. 50 - gave 0 sec., but how 26 - gave 0.003 sec or 45 - gave 0.062 sec. Instead of these results I expected increased seconds by linearly. |
In official documentation: https://docs.python.org/3/library/profile.html was not noted about the difference behaviour of cProfile in command line and into the file. |
This doesn't seem like an appropriate use of the bug tracker. Investigating and explaining cProfile results seems better suited for StackOverflow or some other forum. Then if an actual known bug or potential optimization arises, the bug tracker can to used to follow-up on a specific request. Being "surprised" by timing/profling results usually isn't a bug and can sometimes be resolved by studying the source to how one's simplified mental model differs from the realities faced by a real-world implementation. I recommend closing this but allowing it to be reopened if specific improvement can be proposed. |
@rhettinger, I did not write nowhere in my post that it's a pure bug. I use "behaviour" instead of it. And it getting me incorrect results everytime, that is why I should wrote it in here for resolving this problem. This is not about getting error message, that write to Stackoverflow. I think you were not checking this never. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: