Python Forum
i am wondering if squaring ....
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i am wondering if squaring ....
#1
i am wondering if squaring is faster than multiply in Python's large int arithmetic. i know there is a way, that i cannot recall, to optimize multiplication of a number with itself, mathematically. this cannot be done when the two numbers are different. i'm curious if the big int logic includes this for the case of raising power by 2 making a**2 faster than a*b where b != a but b is nearly as big as a,
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
In [1]: def square(a):
   ...:     a ** 2
   ...:     

In [2]: def multiply(a):
   ...:     a * a
   ...:     

In [3]: %timeit -n 1000000 square(1111)
270 ns ± 1.91 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit -n 1000000 multiply(1111)
97 ns ± 0.801 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: 
If you want speed go try gmpy2 module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
i'll take that as a "no".

actually i had a way to use squaring as an alternative to multiplying for an app. if squaring was faster, it would be worth doing things that way. apparently not. big int is faster in Pike. but I'll stay with Python.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
if anyone is considering optimizing CPython, they should at least look at adding a check in the raise power (**) code to check for x**2 and x**3 (int powers) and do x*x an x*x*x instead of doing it the logarithm way. i doubt it is doing logarithms for big int to int power since i am getting really big int answers with all the right digits.

lt1/forums /home/forums 3> py3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**64
18446744073709551616
>>> 20000000000000000001
20000000000000000001
>>> 20000000000000000001**3
8000000000000000001200000000000000000060000000000000000001
>>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
I don't know what they are doing underneath.
I was playing with really big, big numbers ( few million digits ) and all is fine. I want to play again but this time I will try CUDA to see how it is going to run. I just wonder if uint64 could contain such a number. Or much bigger.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
uint64 (in C) is limited to a maximum of 2**64-1. in C, you can still assign -1 to it an will get that largest possible value. gcc supports a 64 bit int on 32 bit architectures and a 128 bit int on 64 bit architectures. to go beyond that, use libgmp or switch to Python.

in C, x > 0xffffffffffffU would never be true for any possible x that is uint64.

if power takes more time than the equivalent multiply, especially for big int, then power must be doing something else or something more. maybe for small int it is using logarithms. for big powers, that is the fast way. maybe a good test would be to work out the power with logs, and compare that to straight powering.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Loosely related:

>>> -2 ** 2
-4
>>> (-2) ** 2
4
>>> from math import pow
>>> pow(-2, 2)
4
Negative number is not literal. Negative number is unary operator and literal therefore ** takes precedence. pow works in more intuitive way.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
I am asking because I saw examples where in order to use CUDA in Python I have to provide the type of the variables. I do not plan to use C but Python. But I was thinking that because of CPython there will be limitations if I have to say explicitly the types of the variables. I have to review the documentation in more detail. Anyone having fun with CUDA here? Rolleyes
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
i think Python float is limited to 64 bits and hence has a type as CUDA might know them. a really big int would be some kind of array. i'm have no idea how you might need to pass data to CUDA from Python, much less tell CUDA what it is getting. are you going to be running some specific software there that might document this or are you expecting to run your own Python code on it?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
I will run my own Python code. I will stick with the big integers for now.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wondering about a External Website Codezters 9 4,938 Sep-01-2017, 08:19 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020