大数的Python **和%运算符的行为 [英] Behavior of Python ** and % operators with big numbers

查看:119
本文介绍了大数的Python **和%运算符的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Python解释器中放入** b%c时,它具有较大的a(20个数字)b(4个数字)c(20个数字),我发现Python运算速度非常快,几乎与pow(a,b,c ).我希望Python能够先计算出** b,然后再得到结果的模数(%),这将花费更多的时间.

When I put in Python interpreter a ** b % c with large a (20 figures) b (4 figures) c (20 figures) I saw that Python calculates it pretty fast, almost like pow (a,b,c). I expect another behavior that Python first calculate a ** b then get the modulo (%) of result and such calculation will take significantly more time.

幕后的魔力在哪里?

推荐答案

如果要在Python解释器中输入类似内容:

If you are typing into the Python interpreter something like:

20937505974095709374 ** 3438

然后看到几秒钟的等待.然后尝试:

Then seeing a couple of seconds wait. Then trying:

20937505974095709374 ** 3438 % 6

然后看到等待,想知道为什么存在差异,那么您在第一个实例中看到的 delay 实际上是您的终端缓冲并花费的时间.在屏幕上打印刚创建的庞大数字.

And seeing no wait, and wondering why there is a difference, then the delay that you see in the first instance is actually the time your terminal takes to buffer and print the huge number you just created to the screen.

这篇关于大数的Python **和%运算符的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆