【问题标题】:More efficient string to command method更有效的字符串到命令方法
【发布时间】:2019-04-09 08:03:19
【问题描述】:

我需要一种更有效的方法来完成以下工作:

for character in commandSequence:

    if character == "F":
        i += 1
        gps = move(fullList.iloc[i-1, 2:].values,
                   distance, yaw, pitch, 'forward')

        fullList.loc[i] = ['Forward', 'Actuated', gps[0], gps[1], gps[2]]

    if character == "B":
        i += 1
        gps = move(fullList.iloc[i-1, 2:].values,
                   distance, yaw, pitch, 'backward')

        fullList.loc[i] = ['Backwards', 'Actuated', gps[0], gps[1], gps[2]]

    if character == "+":
        yaw = yaw + radians(yaw)

    if character == '-':
        yaw = yaw - radians(yaw)

    if character == "^":
        pitch = pitch + radians(pitch)

    if character == '.':
        pitch = pitch - radians(pitch)

    if character == '[':
        fullList.iloc[i, 0] = 'Branch'

    if character == ']':
        if fullList['Description'].value_counts()['Branch'] > 0:
            fullList.iloc[i, 0] = 'EOL'
            upsideDown = fullList.reindex(
                index=fullList.index[::-1]).dropna()
            temp = upsideDown.iloc[upsideDown['Description'].eq(
                'Branch').idxmax()].values
            i += 1
            fullList.iloc[i] = temp

一个典型的commandSequence如下所示:
FF+[+[FB]-+[FB]-+[FB]-]-FBFF+[+[FB]-+ [FB]-+[FB]-]-FB

我需要优化它,因为它是进化算法的一部分,所以有很多迭代意味着我需要尽可能加快速度。

另外,如果有人可以提供一些指导,我使用 profilestats 中的 profile 来计时我的代码,并输出以下内容:

21233966 function calls (20864585 primitive calls) in 46.433 seconds

   Ordered by: cumulative time
   List reduced from 1994 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.026    0.026   77.205   77.205 attempt_1.2.py:167(main)
        1    0.003    0.003   31.960   31.960 draw.py:1(draw)
        8    0.000    0.000   30.788    3.849 pyplot.py:236(show)
        8    0.000    0.000   30.788    3.849 backend_bases.py:178(show)
        1    0.000    0.000   30.762   30.762 backend_qt5.py:1115(mainloop)
      180    0.406    0.002   27.591    0.153 attempt_1.2.py:32(toCoordinates)
      180    0.284    0.002   17.542    0.097 attempt_1.2.py:104(absorbArea)
27339/19427    0.328    0.000   12.563    0.001 indexing.py:1463(__getitem__)
    12228    0.200    0.000   10.900    0.001 indexing.py:2011(_getitem_tuple)
    25706    0.202    0.000    9.834    0.000 indexing.py:2075(_getitem_axis)

所以我说我需要加快 toCoordinatesabsorbArea 和任何 __getitem__ 调用是否正确(不管它们是什么??)

【问题讨论】:

  • 你可以使用 if character=='F' .... elif charcater=='B' ... elif ... else 快if ... if ... if .... 因为一旦你匹配了一个条件,你就不需要测试其他的了

标签: python performance profilestats


【解决方案1】:

在您的循环中,您可以向每个 if 添加 continue 以避免测试所有其他 if,在这种情况下,循环转到下一个字符。

    if character == "+":
        yaw = yaw + radians(yaw)
        continue

或者使用 if / elif

   if condition1:
      some actions

   elif condition2:

等等

我再也看不到加速你的循环了

【讨论】:

  • 谢谢,我去实现elif的
  • 如果对您有帮助,请不要忘记投票/验证答案
  • 打开它看看是否有人可以帮助处理 profilestats
  • 好的,但是说我需要加速是不正确的,也许你可以加速......分析显示所有系统和应用程序功能的时间。这让你知道从哪里开始搜索,但也许你可以或不能加速函数,例如 getitem 这是一个调用字典值或键的系统函数
  • 感谢 Frenchy,这确实回答了我的问题,因此我无法加快速度 /__getItem__
猜你喜欢
  • 2010-10-16
  • 2020-11-26
  • 2021-08-01
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 2011-08-22
  • 1970-01-01
相关资源
最近更新 更多