What kind of python will faster?

I’m new for Python(and this is my first programming language), making scripts for my game. After some try, I fond that functions will make game slow, so I have better sign them a name such as Sensor = own.getSensor(“sensor”).isPosible() and use Sensor instead of that functions. I still have some other questions:
1.Conditionals such as “if”, “elif” etc. and calculations such as “+”, “<”, “=”, which will be faster?

2.For the same situtation, long description conditional faster or multi conditionals faster?

3.Wil a calculation take any computer time when not it’s condition?

4.Maybe not a Python question, we have 5 kind of properties in game engine, would some of them take much more computer time than others?

Or maybe somebody have more suggestion, thank you.

use the tools that make your code cleaner and easier to maintain, there may be a performance difference but it is NOT worth the extra time.

generally the speed limitation in python is related to the actual execution of code. If you can do something with less code [or avoid doing it at all, or doing it fewer times per second] you will be better off. Even so, if you’re working really hard to make something run quickly enough you’re probably solving the wrong problem.

Even so, if you’re working really hard to make something run quickly enough you’re probably solving the wrong problem.

It depends on what you are doing. If you are doing some serious number crunching, then (forgetting the ‘do it in c’ shouts) then there is one thing that can be very important to remember:
Dots are slow. foo.bar.thing.name.function() called a load of times will be slower that f = foo.bar.thing.name.function then calling f().

I had some code that was sped up ~100 times by removing long links like this.

If you want to see the speed of different things, set up some big loops (use xrange rather than range) and run the profiler on them.

ian

More thought, maybe right or wrong:

  • If I have a series conditions about numbers have to process, write all of them with “if…:” will takes more memory and lower CPU, and write with a “for” loop and one “if…:” will takes lower memory but more CPU.
  • Write many situations in one file with conditions will slower than split them to different file and call them with different sensor only when I need them.One more quetion: I have more than one, or more than one dozen of time have to count, such as weapon attack delay, every character in the scene need to be count. Now I have two ways to do this: give each of them a timer, or only one global timer, every count have to get current time and calc the time they need to finish. As the first question: which will faster?

Have you read this?
http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Optimize

No, and that’s very helpful. Thank you for guide.