为什么我的 python 函数比 gmp 乘法更快?
我的问题是关于标题的。我不解释这个 python 脚本的结果。我认为脚本上没有错误,就像我在 C 脚本中所做的那样。我没有解释为什么它在 C 中不可重现。我做了一个似乎比 gmp 乘法更快的函数...
这是 python 脚本
import gmpy2
from gmpy2 import mpz
import sys
import time
import numpy as np
from math import ceil, floor
n_tests = 200
g=4
bzero=1
czero=1
dzer=1
indice=np.array([1, 8, 11, 14, 20, 23, 26, 29, 35, 38, 41, 48, 50, 53, 60, 63, 68, 71, 74, 77, 83, 86, 89, 96, 98, 101, 108, 111, 113, 120, 123, 126, 131, 134, 137, 144, 146, 149, 156, 159, 161, 168, 171, 174, 180, 183, 186, 189, 194, 197, 204, 207, 209, 216, 219, 222, 228, 231, 234, 237, 243, 246, 249, 256])
v0=np.ones(64)
v1=indice
v2=indice**2
v3=indice**3
t2=mpz('0')
for i in range(64):
h=v1[i]
h2=h**2
h3=h**3
h=mpz(str(h))
h2=mpz(str(h2))
h3=mpz(str(h3))
t2=gmpy2.add(gmpy2.add(gmpy2.add(t2,h),h2),h3)
# t2=gmpy2.add(t2,h3)
a=256
b=256*257/2
c=np.sum((np.arange(1, (g ** g+1), 1))**2)
d=np.sum((np.arange(1, (g ** g+1), 1))**3)
t=b+c+d
X1=mpz('6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151')**1457
X2=mpz('531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127')**16711
print(t)
print(t2)
def mult_python(x1, x2):
return gmpy2.mul(x1,x2)
def mult_david(x1, x2):
azero=gmpy2.div(gmpy2.sub(gmpy2.mul(x1,256),t),256)
aprime=gmpy2.mul(azero,64)
x=gmpy2.add(aprime,t2)
azeroprime=gmpy2.div(gmpy2.sub(gmpy2.mul(x2,256),t),256)
aprime=gmpy2.mul(azeroprime,64)
y=gmpy2.add(aprime,t2)
return gmpy2.div(gmpy2.mul(x,y),4096)
mult_functions = [mult_python, mult_david]
mult_times = {mult:[] for mult in mult_functions}
mult_errors = {mult:[] for mult in mult_functions}
N_ref = mult_python(X1,X2)
for i in range(n_tests):
for mult in mult_functions:
start_time = time.time()
N = mult(X1, X2)
end_time = time.time()
mult_times[mult] += [end_time - start_time]
mult_errors[mult] += [gmpy2.sub(N,N_ref)]
sys.stdout.write('\r')
sys.stdout.write("[{:50s}] {:d}%".format('='*(int(50*(i+1)/n_tests)), int(100*(i+1)/n_tests)))
sys.stdout.flush()
for mult in mult_functions:
print("{:15s}: {:e}s [error={}]".format(mult.__name__,
np.mean(mult_times[mult]),
np.mean(mult_errors[mult])))
print("ma methode est tant de fois plus rapide",np.mean(mult_times[mult_python])/np.mean(mult_times[mult_david]))
My question is on the title. I don't explain the result of this python script. I think that there is no error on the script as I have made in my C script. I don't explain why it is not reproductable in C. I made a function that's seems to be faster than gmp multiplication...
here is the python script
import gmpy2
from gmpy2 import mpz
import sys
import time
import numpy as np
from math import ceil, floor
n_tests = 200
g=4
bzero=1
czero=1
dzer=1
indice=np.array([1, 8, 11, 14, 20, 23, 26, 29, 35, 38, 41, 48, 50, 53, 60, 63, 68, 71, 74, 77, 83, 86, 89, 96, 98, 101, 108, 111, 113, 120, 123, 126, 131, 134, 137, 144, 146, 149, 156, 159, 161, 168, 171, 174, 180, 183, 186, 189, 194, 197, 204, 207, 209, 216, 219, 222, 228, 231, 234, 237, 243, 246, 249, 256])
v0=np.ones(64)
v1=indice
v2=indice**2
v3=indice**3
t2=mpz('0')
for i in range(64):
h=v1[i]
h2=h**2
h3=h**3
h=mpz(str(h))
h2=mpz(str(h2))
h3=mpz(str(h3))
t2=gmpy2.add(gmpy2.add(gmpy2.add(t2,h),h2),h3)
# t2=gmpy2.add(t2,h3)
a=256
b=256*257/2
c=np.sum((np.arange(1, (g ** g+1), 1))**2)
d=np.sum((np.arange(1, (g ** g+1), 1))**3)
t=b+c+d
X1=mpz('6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151')**1457
X2=mpz('531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127')**16711
print(t)
print(t2)
def mult_python(x1, x2):
return gmpy2.mul(x1,x2)
def mult_david(x1, x2):
azero=gmpy2.div(gmpy2.sub(gmpy2.mul(x1,256),t),256)
aprime=gmpy2.mul(azero,64)
x=gmpy2.add(aprime,t2)
azeroprime=gmpy2.div(gmpy2.sub(gmpy2.mul(x2,256),t),256)
aprime=gmpy2.mul(azeroprime,64)
y=gmpy2.add(aprime,t2)
return gmpy2.div(gmpy2.mul(x,y),4096)
mult_functions = [mult_python, mult_david]
mult_times = {mult:[] for mult in mult_functions}
mult_errors = {mult:[] for mult in mult_functions}
N_ref = mult_python(X1,X2)
for i in range(n_tests):
for mult in mult_functions:
start_time = time.time()
N = mult(X1, X2)
end_time = time.time()
mult_times[mult] += [end_time - start_time]
mult_errors[mult] += [gmpy2.sub(N,N_ref)]
sys.stdout.write('\r')
sys.stdout.write("[{:50s}] {:d}%".format('='*(int(50*(i+1)/n_tests)), int(100*(i+1)/n_tests)))
sys.stdout.flush()
for mult in mult_functions:
print("{:15s}: {:e}s [error={}]".format(mult.__name__,
np.mean(mult_times[mult]),
np.mean(mult_errors[mult])))
print("ma methode est tant de fois plus rapide",np.mean(mult_times[mult_python])/np.mean(mult_times[mult_david]))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论