top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Does Python optimize low-power functions?

+1 vote

The following two functions return the same result:


But they may be computed in different ways. The first choice can accommodate non-integer powers and so it would logically proceed by taking a logarithm, multiplying by the power (in this case, 2), and then taking the anti-logarithm. But for a trivial value for the power like 2, this is clearly a wasteful choice. Just multiply x by itself, and skip the expensive log and anti-log steps.

My question is, what do Python interpreters do with power operators where the power is a small constant, like 2? Do they know to take the shortcut?

posted Dec 6, 2013 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
It is probably specific to the interpreter implementation(cython, jython, iron python etc...). You'd better optimize it yourself should you really care about this.

An alternative is to use numpy functions, like numpy.power, they are optimized version of most mathematical functions.

1 Answer

+1 vote

It's worth noting that the *interpreter* per se is not doing this. The implementation of the long object does this in its implementation of the __pow__ method, which the interpreter invokes. Other objects may implement this differently and use whatever optimizations they like. They may even (ab)use the syntax for things other than numerical exponentiation where x**2 is not equivalent to x*x. Since objects are free to do so, the interpreter itself cannot choose to optimize that exponentiation down to multiplication.

answer Dec 6, 2013 by Majula Joshi
Similar Questions
+3 votes

I know in optimizing a LTE network that we could measure RSRP,RSRQ ,Power and other conditions, but if all things goes right and still I am getting drop so how could I optimize it from UE side?

+1 vote

I was trying to optimize the InnoDB tables. I have executed the next query to detect what are the fragmented tables.

FROM TABLES WHERE TABLE_SCHEMA NOT IN ("information_schema","mysql") AND Data_free > 0

After that, I have seen that there are 49 fragmented tables. With one table, I have executed "optimize table table_name;" and "analyze table table_name;". The result is the same, the table continuous fragmented.

Any suggestions? I have followed the mysqltuner recommendations...

0 votes

I am looking for detailed description of optimization flags that are provided by GCC compiler ?

+2 votes

I want to use some loop optimizations. I have build GCC-4.8.1 using gmp 5.1.3, mpfr 3.1.2, mpc 1.0.1, cloog 0.18.0 and isl 0.11.1. I tried optimization flags -floop-interchange and -floop-block. I don't find any changes in the optimized code.

Are graphite loop transforms implemented in GCC-4.8.1. Should I configure and build GCC in a different way.

+5 votes

I have a C code like this:

int foo(void)
 int phase;
 . . .
 phase = 1;
 phase = 2;
 phase = 3;
 . . .

In case of -O0 gcc generates machine instructions for every assignment 'phase = ...'. But in case of -O2 gcc does not generate instructions for some assignments. Of course, this is correct. However, is there any way to tell gcc that 'phase' object is inspected by another thread, so it should not remove such statements?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.