3

这是我的script.py

@profile
def primes(n): 
    if n == 2:
        return [2]
    elif n < 2:
        return []

    s = range(3, n + 1, 2)
    mroot = n ** 0.5
    half = (n + 1) / 2 - 1

    i = 0
    m = 3
    while m <= mroot:
        if s[i]:
            j = (m ** 2 - 3) / 2
            s[j] = 0
            while j < half:
                s[j] = 0
                j += m
        i += 1
        m = 2*i + 3

    return [2] + [x for x in s if x]

number = 40000
elts = primes(number)

我安装了line_profiler

$ pip install line_profiler

并按如下方式运行

$ kernprof -l -v script.py
Wrote profile results to profiling.py.lprof
Timer unit: 1e-06 s

这很奇怪......没有按行计时的结果!

所以我查看了转储,但也没有运气:

$ python -m line_profiler profiling.py.lprof
Timer unit: 1e-06 s

任何想法我如何错误地使用这个库?

4

0 回答 0