为什么我的 python 函数比 gmp 乘法更快?

发布于 2025-01-09 18:33:46 字数 2662 浏览 3 评论 0原文

我的问题是关于标题的。我不解释这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文