0

I need a more efficient method to accomplish the following:

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

A typical commandSequence looks like this:
FF+[+[FB]-+[FB]-+[FB]-]-FBFF+[+[FB]-+[FB]-+[FB]-]-FB

I need to optimize this as it is part of an evolutionary algorithm so there's lots of iterations meaning I need to speed up wherever I can.

Also, if anyone could provide some guidance, I used profile from profilestats to time my code and the following was output:

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)

So am I correct in saying I need to speed up toCoordinates, absorbArea and any __getitem__ calls (whatever those are??)

Izak Joubert
  • 906
  • 11
  • 29
  • 1
    You can use **if character=='F' .... elif charcater=='B' ... elif ... else** is faster than **if ... if ... if ....** because once you match a condition, you don't need to test the other ones – nacho Apr 09 '19 at 08:08

1 Answers1

1

In your loop you could add continue to each if to avoid the test of all others if, in this case the loop goes to next char.

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

or using if / elif

   if condition1:
      some actions

   elif condition2:

and so on

i dont see anymore to speedup your loop

Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • Thank you, I'll implement the elif's – Izak Joubert Apr 09 '19 at 08:22
  • if that helps you, dont forget to upvote/validate the answer please – Frenchy Apr 09 '19 at 09:32
  • Just leaving it open to see if someone can help with the profilestats – Izak Joubert Apr 09 '19 at 10:01
  • ok, but its not correct to say i need to speedup, maybe you could speedup... the profiling shows all system and application functions timings. That gives you an idea where to begin to search, but maybe you could or couldnt speedup the function like for example __getitem__ which is a system function whichs calls the dictionary value or key – Frenchy Apr 09 '19 at 10:44
  • Thanks Frenchy, that actually does answer my question, thus there is nothing I can do to speed up /__getItem__ – Izak Joubert Apr 10 '19 at 08:01