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
