代码优化(计算PI) [英] code optimization (calc PI)

查看:110
本文介绍了代码优化(计算PI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(是的,我知道一个对象是......)

BTW。我用C语言编写了一个pi调用程序到Python的翻译。

(由你自己做... ;-)

--------

计算PI为800挖(?)。 (德语:Stellen)

------

int a = 10000,b,c = 2800,d,e,f [2801],g; main (){for(; bc;)f [b ++] = a / 5;

for(; d = 0,g = c * 2; c- = 14,printf("%。 4d,e + d / a),e = d%a)for(b = c; d + = f [b] * a,

f [b] = d% - g,d / = g - , - b; d * = b);}


$ ./a.exe

31415926535897932384626433832795028841971693993751 058209749445923078164062862089

98628034825342117067982148086513282306647093844609 550582231725359408128481117450

28410270193852110555964462294895493038196442881097 566593344612847564823378678316

52712019091456485669234603486104543266482133936072 602491412737245870066063155881

74881520920962829254091715364367892590360011330530 548820466521384146951941511609

43305727036575959195309218611738193261179310511854 807446237996274956735188575272

48912279381830119491298336733624406566430860213949 463952247371907021798609437027

70539217176293176752384674818467669405132000568127 14526356082 7785771342757789609

17363717872146844090122495343014654958537105079227 968925892354201995611212902196

08640344181598136297747713099605187072113499999983

--------


但是Python在这里比C慢得多。我在

while循环中使用了while循环。这里的计算不多。

(Yes, I konw whats an object is...)
BTW. I did a translation of a pi callculation programm in C to Python.
(Do it by your own... ;-)
--------
Calc PI for 800 digs(?). (german: Stellen)
------
int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}

$ ./a.exe
31415926535897932384626433832795028841971693993751 058209749445923078164062862089
98628034825342117067982148086513282306647093844609 550582231725359408128481117450
28410270193852110555964462294895493038196442881097 566593344612847564823378678316
52712019091456485669234603486104543266482133936072 602491412737245870066063155881
74881520920962829254091715364367892590360011330530 548820466521384146951941511609
43305727036575959195309218611738193261179310511854 807446237996274956735188575272
48912279381830119491298336733624406566430860213949 463952247371907021798609437027
70539217176293176752384674818467669405132000568127 145263560827785771342757789609
17363717872146844090122495343014654958537105079227 968925892354201995611212902196
08640344181598136297747713099605187072113499999983 729780499510597317328160963185
--------

But Python is much slower here then C here. I used a while-loop within a
while-loop. Not that much calculation here.

推荐答案

./ a.exe

31415926535897932384626433832795028841971693993751 058209749445923078164062862089

98628034825342117067982148086513282306647093844609 550582231725359408128481117450

28410270193852110555964462294895493038196442881097 566593344612847564823378678316

52712019091456485669234603486104543266482133936072 602491412737245870066063155881

74881520920962829254091715364367892590360011330530 548820466521384146951941511609

43305727036575959195309218611738193261179310511854 807446237996274956735188575272

48912279381830119491298336733624406566430860213949 463952247371907021798609437027

70539217176293176752384674818467669405132000568127 145263560827785771342757789609

17363717872146844090122495343014654958537105079227 968925892354201995611212902196

0864034418159813629774771309960518707211349999 9983 729780499510597317328160963185

--------


但是这里的C比C慢得多。我在

while循环中使用了while循环。这里计算不多。
./a.exe
31415926535897932384626433832795028841971693993751 058209749445923078164062862089
98628034825342117067982148086513282306647093844609 550582231725359408128481117450
28410270193852110555964462294895493038196442881097 566593344612847564823378678316
52712019091456485669234603486104543266482133936072 602491412737245870066063155881
74881520920962829254091715364367892590360011330530 548820466521384146951941511609
43305727036575959195309218611738193261179310511854 807446237996274956735188575272
48912279381830119491298336733624406566430860213949 463952247371907021798609437027
70539217176293176752384674818467669405132000568127 145263560827785771342757789609
17363717872146844090122495343014654958537105079227 968925892354201995611212902196
08640344181598136297747713099605187072113499999983 729780499510597317328160963185
--------

But Python is much slower here then C here. I used a while-loop within a
while-loop. Not that much calculation here.


mm写道:
mm wrote:

(是的,我知道对象是什么。 ..)

BTW。我用C语言将一个pi调用程序翻译成了Python。

(由你自己做... ;-)
(Yes, I konw whats an object is...)
BTW. I did a translation of a pi callculation programm in C to Python.
(Do it by your own... ;-)



这是一个关于如何优化代码的问题,你不会告诉我们吗?如果是的话,我是

抱歉告诉你这些天水晶球很短。太多了

新年展望。


Diez

Is that a question on how to optimize code you won''t show us? If yes, I''m
sorry to tell you that crystal balls are short these days. Too much
new-year-outlooks.

Diez




嗯......这是个问题。将这个#@ *?%!

C-Program翻译成可读代码然后再翻译成Python并不容易。但它确实有效。


只有两个while循环(在另一个循环中有一段时间)。


我知道,例如Perl中的while循环非常慢。也许这个

在Pyhton中也是众所周知的。然后,我可以将while循环转换为

for-loops,例如。

更一般地说,也许有一个速度优化文档。


(无论如何,这个C-calc的代码很复杂。我真的不明白

现在...... 8-)

但无论如何,没有那么多的计算,它有更多的东西来支持
。以下是一些代码:


(一般情况下,它与PI-calc基本无关。)

c = 2800 ## a counter

而c * 2:

##做一些计算。这里没有改变。

...

b = c ##元素数量


而(b-1) :

##所以有些计算结果。

...

b = b-1 ##仅适用于while循环条件。

c = c-14; ##这是代码vor第一个while循环,BUT胸围在

循环后运行。

pi = pi + str("%04d" %int(e + d / a))##这应该快吗?!我不知道
知道。

有一个输出字符串,一个列表和一个整数。没有复杂的数据

结构;没有复杂的计算。


Diez B. Roggisch写道:

Hmm... it''s a question. It was not that easy to translate this #@*?%!
C-Program into readable code and then to Python. But it works.

There are only two while-loops (a while within an other while).

I konw, that for example while-loops in Perl are very slow. Maybe this
is also known in Pyhton. Then, I can translate the while-loops in to
for-loops, for example.
More general, maybe there is a speed optimazation docu out there.

(Anyway, this code for C-calc is complex. And I realy don''t understand
it right now... 8-)
But anyway, there is not that much calculation, it has more something to
do with the too while-loops. Here is some code:

(In general, it has basically nothing to do with PI-calc.)
c=2800 ## a counter
while c*2:
## do some calc. did not change c here.
...
b=c ## number of elements

while (b-1):
## so some calc.
...
b=b-1 ## just for while-loop condition.
c = c-14; ## this is code vor the 1st while-loop, BUT bust run after
the 2nd-while-loop.
pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont
know.
There are a output string, a list, and integers. No complex data
structures; and no complex calculations.

Diez B. Roggisch wrote:

mm写道:

mm wrote:


>>(是的,我知道对象是什么......)
BTW。我用C语言编写了一个pi调用程序到Python的翻译。
(由你自己做... ;-)
>>(Yes, I konw whats an object is...)
BTW. I did a translation of a pi callculation programm in C to Python.
(Do it by your own... ;-)




这是一个关于如何优化代码的问题,你不会告诉我们吗?如果是的话,我是

抱歉告诉你这些天水晶球很短。太多了

新年前景。


Diez



Is that a question on how to optimize code you won''t show us? If yes, I''m
sorry to tell you that crystal balls are short these days. Too much
new-year-outlooks.

Diez


这篇关于代码优化(计算PI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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