## 加快python -c调用内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (11)

/usr/bin/python -c "from sympy import Intersection; from sympy import solveset; from sympy import S; from sympy.abc import x; from sympy.functions.elementary.miscellaneous import Min, Max; print Intersection(*[solveset(p, x, S.Reals) for p in [(x > 4.0000), (x < 6.0000)]])" 2>&1

/usr/bin/python -c "from sympy import Intersection; from sympy import solveset; from sympy import S; from sympy.abc import x; from sympy.functions.elementary.miscellaneous import Min, Max; print Intersection(*[solveset(p, x, S.Reals) for p in [(x > 4.0000), (x < 6.0000), (((x) * 4.0000 + 5.0000) > 5.0000)]])" 2>&1

/usr/bin/python -c "from sympy import Intersection; from sympy import solveset; from sympy import S; from sympy.abc import x; from sympy.functions.elementary.miscellaneous import Min, Max; print Intersection(*[solveset(p, x, S.Reals) for p in [(x > 4.0000), (x < 6.0000), ((x * (Min(Max(x, 4.0000), 5.0000))) > 7.0000), ((Min(Max(x, 4.0000), 5.0000)) > 5.0000)]])" 2>&1

# time /usr/bin/python -c "from sympy import Intersection; from sympy import solveset; from sympy import S; from sympy.abc import x; from sympy.functions.elementary.miscellaneous import Min, Max; print Intersection(*[solveset(p, x, S.Reals) for p in [(x > 4.0000), (x < 6.0000), ((x * (Min(Max(x, 4.0000), 5.0000))) > 7.0000), ((Min(Max(x, 4.0000), 5.0000)) > 5.0000)]])"
EmptySet()

real    0m3.080s
user    0m2.920s
sys 0m0.050s

# time pypy -c "from sympy import Intersection; from sympy import solveset; from sympy import S; from sympy.abc import x; from sympy.functions.elementary.miscellaneous import Min, Max; print Intersection(*[solveset(p, x, S.Reals) for p in [(x > 4.0000), (x < 6.0000), ((x * (Min(Max(x, 4.0000), 5.0000))) > 7.0000), ((Min(Max(x, 4.0000), 5.0000)) > 5.0000)]])"
EmptySet()

real    0m6.816s
user    0m6.660s
sys 0m0.080s

### 2 个回答

SymPy启动缓慢但速度不慢。你看到的缓慢只是你的特定方法在SymPy下很慢。通过使用int而不是浮点数，您可以使示例更快一些。

In [7]: (x<4).as_set()
Out[7]: (-∞, 4)

In [11]: piecewise_fold(Min(Max(x, 4.0000), 5.0000).rewrite(Piecewise))
Out[11]:
⎧5.0  for x ≥ 5.0
⎪
⎨4.0  for x ≤ 4.0
⎪
⎩ x    otherwise