0

我试着跑python -m cProfile simple_test_script.py。我在 Windows 7,Python 2.7.10 上。

simple_test_script.py:

import numpy as np
from numpy.linalg import eigvals

def run_experiment(niter=100):
    K = 100
    results = []
    for _ in xrange(niter):
        mat = np.random.randn(K, K)
        max_eigenvalue = np.abs(eigvals(mat)).max()
        results.append(max_eigenvalue)
    return results
some_results = run_experiment()
print 'Largest one we saw: %s' % np.max(some_results)

我收到此错误:

File "<ipython-input-13-6634cb53f497>", line 1
    python -m cProfile simple_test_script.py
                     ^
SyntaxError: invalid syntax

我阅读了这份文档:https ://docs.python.org/2/library/profile.html

(如果 cProfile 在您的系统上不可用,请使用 profile 而不是 cProfile。)

我尝试了 profile 而不是 cProfile 但同样的错误。有什么线索可以调用 cProfile 吗?

4

2 回答 2

4

您似乎在 IPython 中运行以下命令:

python -m cProfile simple_test_script.py

你应该在你的shell中运行它。

于 2015-05-25T08:50:49.137 回答
3

正如 satoru 建议的那样,您通常会在 shell/终端/控制台中运行这样的命令(对于日常使用,这些基本上意味着相同的事情)。但是,您可以从 IPython 内部运行它,例如:

%run -m cProfile simple_test_script.py

(% 符号是命令的一部分,IPython 有一些以 % 开头的特殊命令)

于 2015-05-25T08:57:51.980 回答