473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Multiplication optimization

Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting? That is, for a
product a*b, is there any shortcutting of (potentially expensive)
multiplication operations as in:

if a == 0
return 0
if a == 1
return b
return a*b

Of course, for the cases where a is not 0 or 1 we are adding two
comparisons to each multiply operation. But in some applications
(sparse matrices? binary conversion?), the multiplication step could be
avoided in many cases, if the user was aware that left-to-right
short-cutting were implemented.

Or by adding two *more* comparisons, then all multiplications would be
"optimized":

if a == 0 or b == 0
return 0
if a == 1
return b
if b == 1
return a
return a*b

But this seems overkill, especially since boolean short-cutting only
works left-to-right.

Just curious...

-- Paul

Feb 19 '06 #1
5 1547
On Sat, 18 Feb 2006 16:48:38 -0800, Paul McGuire wrote:
Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting?


Do you know that these shortcuts are optimizations, or are you just
assuming it takes less time to do the comparison than it would for the
CPU to blast bits around?

I have no idea whether your shortcuts are optimizations or pessimizations.
I'm just asking whether you know, or if you are making assumptions.
--
Steven.

Feb 19 '06 #2
[Paul McGuire]
Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting?


Usually, it is safest (and typically true) to assume that Python
performs no optimizations. To go beyond making assumptions, it is easy
to run a few timings:
from timeit import Timer
min(Timer('0*1234567').repeat(5)) 0.11315376674968469 min(Timer('1*1234567').repeat(5)) 0.11920453577200618 min(Timer('113*1234567').repeat(5))

0.11881482143680699

In your specific case, I can answer than the source shows no special
optimization for multiplications by specific values. There is a
short-path for integer multiplications but it is not value specific.
In Py2.5, the compiler will do a limited amount of constant folding;
however, its scope is somewhat limited because expressions like a*3
vary depending on the a's type which is often not knowable by the
compiler without some form of global analysis.

One other thought: Python is not assembly. Run time is unlikely to be
dominated by the time to execute a multiplication. With Python, it is
best to focus optimization efforts on making choosing the best data
structures and hoisting constants out of loops.
Raymond

Feb 19 '06 #3
Paul McGuire wrote:
Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting? That is, for a
product a*b, is there any shortcutting of (potentially expensive)
multiplication operations


no. and the reason is very simple: to the extent such optimization
makes sense, it has been done on assembler/CPU level already. i.e. when
the multiplication is mapped to the machine code
IMUL <op>
the Pentium processor would be smart enough not to do the work if AX or
the op are 0 or 1.

Python has no job trying to outsmart Intel (and the other chipmakers).
Boolean shortcuts are useful for entirely different reason, not speed.
e.g.
if lastDigit == 0 or i % lastDigit != 0:
break

if both operands of OR were to be evaluated, that would end up with
division-by-zero exception for lastDigit=0.

Feb 19 '06 #4
Atanas Banov wrote:
Paul McGuire wrote:
Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting? That is, for a
product a*b, is there any shortcutting of (potentially expensive)
multiplication operations


no. and the reason is very simple: to the extent such optimization
makes sense, it has been done on assembler/CPU level already. i.e. when
the multiplication is mapped to the machine code
IMUL <op>
the Pentium processor would be smart enough not to do the work if AX or
the op are 0 or 1.

Python has no job trying to outsmart Intel (and the other chipmakers).
Boolean shortcuts are useful for entirely different reason, not speed.
e.g.
if lastDigit == 0 or i % lastDigit != 0:
break

if both operands of OR were to be evaluated, that would end up with
division-by-zero exception for lastDigit=0.


The point is not to avoid the multiplication on the CPU level but the object
creation overhead:
a = 1234
1 * a is a

False # could be True
$ python -m timeit -s'a=1;b=1234567' 'if a is 1: x = b' 'else: x = a * b'
10000000 loops, best of 3: 0.171 usec per loop
$ python -m timeit -s'a=1;b=1234567' 'x = a * b'
1000000 loops, best of 3: 0.211 usec per loop
$ python -m timeit -s'a=2;b=1234567' 'if a is 1: x = b' 'else: x = a * b'
1000000 loops, best of 3: 0.329 usec per loop

If 'a is 1' is sufficiently likely, that test speeds up the code even in
pure Python. Of course a generalized implementation would also have to
check b's type:

$ python -m timeit -s'a=1;b=1234567' 'if a is 1 and b.__class__ is int: x =
b' 'else: x = a * b'
1000000 loops, best of 3: 0.484 usec per loop

So that is not a good idea, at least in pure Python.

Peter

Feb 20 '06 #5
"Paul McGuire" <pt***@austin.rr.com> wrote:

Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting? That is, for a
product a*b, is there any shortcutting of (potentially expensive)
multiplication operations as in:


Integer multiplication is a 1-cycle operation in Intel processors.

Even multiple-precision multiplication is very efficient -- probably more
so than multiple comparisons and jumps.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Feb 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: I.V. Aprameya Rao | last post by:
hi i have been wondering, how does python store its very long integers and perform aritmetic on it. i needed to implement this myself and was thinking of storing the digits of an integer in a...
11
by: Michael Bader | last post by:
Hi, I'm currently working on a matrix multiplication code (matrix times matrix), and have come along some interesting/confusing results concerning the running time of the (apparently) same...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
4
by: Yixin Cao | last post by:
GCC always avoids multiplication instructions as much as possible, so a *= 10; will be translated into: leal (%eax, %eax, 4), %eax add %eax, %eax I do know that it is due to historic...
7
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.