I want to learn how to profile the code in PyCharm in windows 7. But I just can’t open .prof files in Windows. As far as I understood all the profiling tools for Linux. How can I open a .prof file for a normal code review so to speak. I want to speed up this code: the task is to calculate the sum of binomial coefficients on a given diagonal. The sum on the diagonal 0 is equal to 8 (S (7, 0), 7 is the line number in which we start, 0 is the number of the diagonal). Similarly, S (7, 1) is 28, S (7, 2) is 56. You need to write a program that calculates S (n, p), where n is the line where we start and p is the number of the diagonal. Here is the code:

def diagonal(n, k): def pow_binomial(n, k): def eratosthenes(N): simp = [2] nonsimp = set() for i in range(3, N + 1, 2): if i not in nonsimp: nonsimp |= {j for j in range(i * i, N + 1, 2 * i)} simp.append(i) return simp def calc_pow_in_factorial(a, p): res = 0 while a > 0: a //= p res += a return res simple = eratosthenes(n + 1) n_fact_pows = {p: calc_pow_in_factorial(n, p) for p in simple} k_fact_pows = {p: calc_pow_in_factorial(k, p) for p in simple} nmk_fact_pows = {p: calc_pow_in_factorial(n - k, p) for p in simple} ans = 1 for p in simple: ans *= p ** (n_fact_pows[p] - k_fact_pows[p] - nmk_fact_pows[p]) return ans out = 0 while n > 0: f = n - k out += (round(pow_binomial(n, f))) n -= 1 return out 

It starts to slow down if you run a function with such arguments -n = 1921, k = 5

    2 answers 2

    In addition, we can get statistics

     >>> import pstats >>> p = pstats.Stats('<твой путь к файлу статистики>\<какой то там>.prof') >>> p.sort_stats('calls').print_stats() 
    • Thanks, check later. - Linxusr
     import cProfile .... def profile(func): """Decorator for run function profile""" def wrapper(*args, **kwargs): profile_filename = func.__name__ + '.prof' profiler = cProfile.Profile() result = profiler.runcall(func, *args, **kwargs) profiler.dump_stats(profile_filename) return result return wrapper ... @profile def ТвояГлавнаяФункцияИлиТОЧтоНадоПрофилировать(): блаБлаБла f() ff() ... 

    Use Graphviz

    If the question is gprof2dot, then take it here.

    gprof2dot.exe -oc: \ <your path> \ s.res -f pstats c: \ <your path> \ test_cmu.prof

    c: \ Program Files (x86) \ Graphviz2.38 \ bin \ dot.exe -Tpng -Tps c: \ <your path> \ s.res -oc: \ <your path> \ test_cmu.png

    Here is an example of my search.

    enter image description here