Python script speed

Hi all,

Quick question: when using Python scripts, are there certain expressions, functions etc that make the script slower at run-time, or is it a case of just keeping the script as short as possible?

Are there any Python features that beginners like me need to avoid in Blender scripts?

Just curious.

Cheers

Paul

always try to avoid while loops. they will interrupt the game engine.
also time.sleep is to avoid.
Take care on your functions: you can leave a function by typing “return”.
Use break to exit loops.
Use Dictionarys instead of lists…
Use the module controller.
If possible and painless, use the logic bricks, they run faster than a script.
Dont use pulse modes if not necessary, if, use the tap function.

Lists would seem to be faster than dictionaries, since they are ordered sequentially, rather than by index. Are you sure dictionaries are faster?

The rest I agree with. Also, external Python files compile, which means they execute faster than scripts that are internal to Blender. Usually, it’s faster to loop through a set of objects in-game and calculate their logic for them than to have a script on each object in the game.

Comparison List, Collections, Set, Dict.
http://wiki.python.org/moin/TimeComplexity

@ SolarLune
On my computer LoopLogic is running slower than the EachLogic because the rasterizer calculation time in EachLogic increasing form 3.4ms to 15.4ms.

Windows XP
Blender 2.59.0
Intel Q9300
Nvidia Geforce GTX 460

Thanks, I wondered as I recently started making a script to follow a node path. The ideal solution contained an iterable list (I think thats the term) to sequentially go through node objects and set then as targets.

I then made another script that just used if statements to point to nodes when a property couner reached a certain number. It was a longer script, but I guess simpler, and it used about.2/.3 less logic time. This got me wondering if complexity (i.e. the right way) is better than simplicity / logic bricks.

The reason is probably that it’s a script and run the init every time. If You make it a module and create the list only once I pretty sure the looping one is faster.

In an interpreted language You generally want to minimize the number of statement executed. If You loop through a list and have a if statement in the loop body discharging some item. Then You are better of creating a list containing only the item You want - The loop need to loop through less item (the loop statement costs for every turn) and You don’t need the if at all. Ideally You create the list at as few times as possible and reuse it.

The best is if You can capture the state by setting the actual data (that can be functions and objects) and avoid flags. That way the logic can be kept simple.

I agree with LaH - perhaps something is different about your script / set-up that prevents it from working, because the overhead associated with looping through many objects is far less than the overhead associated with having the equivalent script executing on many objects, thereby making Loop Logic faster.

@SolarLune. I used your file for testing. I did not change any thing.