为什么这个循环繁重的代码在Python中如此之慢?可能的项目Euler剧透 [英] Why is this loop heavy code so slow in Python? Possible Project Euler spoilers

查看:59
本文介绍了为什么这个循环繁重的代码在Python中如此之慢?可能的项目Euler剧透的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,但我很高兴。除了使用

它在工作中我一直用它来解决项目中的各种难题

Euler网站 - http://projecteuler.net 。到目前为止,它并没有让我失望,

,但事实证明,它在一个难题上出乎意料地缓慢。


谜题是:p是右边的边界角度三角形

积分长度边,{a,b,c}。 p的值是多少? 1000,

解决方案的数量{a,b,c}最大化了吗?


这里是我的python代码:


#!/ usr / local / bin / python

解决方案= [0] * 1001

p = 0


表示in xrange(1,1000):

for b in xrange(1,1000-a):

表示c in xrange( 1,1000 - a - b):

p = a + b + c

如果p< 1000:

如果** 2 + b ** 2 == c ** 2:

解决方案[p] + = 1


max = 0

maxIndex = 0

index = 0

解决方案解决方案:

如果解决方案最大:

max =解决方案

maxIndex = index

指数+ = 1


打印maxIndex

2.4GHz Core2Duo MacBook需要2分12秒

Pro。感到惊讶的是我实施了相同的算法在

C:


#include< stdio.h>

#include< stdlib.h>


int main(){

int * solutions = calloc(1000,sizeof(int));


int p;

for(int a = 1; a< 1000; ++ a){

for(int b = 1; b <1000 - a; ++ b){

for(int c = 1; c <1000 - a - b; ++ c){

p = a + b + c;

if(p <1000){

if(a * a + b * b == c * c){

解决方案[p] + = 1;

}

}

}

}

}


int max = 0;

int maxIndex = 0;


for(int i = 0; i< 1000; ++ i){

if(solutions [i] max){

max = solutions [i];

maxIndex = i;

}

}

printf("%d \ n",maxIndex );

返回0;

}

gcc -o 39 -std = c99 -O3 39.c

结果执行utable需要0.24秒才能运行。我不希望使用脚本语言比本机代码运行得更快,但我感到惊讶于在这种情况下它的速度有多慢。关于什么

在上面的代码中导致python这么麻烦的任何想法?

I''m pretty new to python, but am very happy with it. As well as using
it at work I''ve been using it to solve various puzzles on the Project
Euler site - http://projecteuler.net. So far it has not let me down,
but it has proved surprisingly slow on one puzzle.

The puzzle is: p is the perimeter of a right angle triangle with
integral length sides, {a,b,c}. which value of p < 1000, is the
number of solutions {a,b,c} maximised?

Here''s my python code:

#!/usr/local/bin/python

solutions = [0] * 1001
p = 0

for a in xrange(1, 1000):
for b in xrange(1, 1000 - a):
for c in xrange(1, 1000 - a - b):
p = a + b + c
if p < 1000:
if a ** 2 + b ** 2 == c ** 2:
solutions[p] += 1

max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1

print maxIndex
It takes 2 minutes and twelve seconds on a 2.4GHz Core2Duo MacBook
Pro. Surprised at how slow it was I implemented the same algorithm in
C:

#include <stdio.h>
#include <stdlib.h>

int main() {
int* solutions = calloc(1000, sizeof(int));

int p;
for(int a = 1; a < 1000; ++a) {
for(int b = 1; b < 1000 - a; ++b) {
for(int c = 1; c < 1000 - a - b; ++c) {
p = a + b + c;
if(p < 1000) {
if(a * a + b * b == c * c) {
solutions[p] += 1;
}
}
}
}
}

int max = 0;
int maxIndex = 0;

for(int i = 0; i < 1000; ++i) {
if(solutions[i] max) {
max = solutions[i];
maxIndex = i;
}
}
printf("%d\n", maxIndex);
return 0;
}
gcc -o 39 -std=c99 -O3 39.c

The resulting executable takes 0.24 seconds to run. I''m not expecting
a scripting language to run faster than native code, but I was
surprised at how much slower it was in this case. Any ideas as to what
is causing python so much trouble in the above code?

推荐答案

On 9月2日下午12:51,jwrweather ... @ gmail.com写道:
On Sep 2, 12:51 pm, jwrweather...@gmail.com wrote:

我对python很新,但我很满意。除了使用

它在工作中我一直用它来解决项目中的各种难题

Euler网站-http://projecteuler.net。到目前为止,它并没有让我失望,

,但事实证明,它在一个难题上出乎意料地缓慢。


谜题是:p是右边的边界角度三角形

积分长度边,{a,b,c}。 p的值是多少? 1000,

解决方案的数量{a,b,c}最大化了吗?


这里是我的python代码:


#!/ usr / local / bin / python

解决方案= [0] * 1001

p = 0


表示in xrange(1,1000):

for b in xrange(1,1000-a):

表示c in xrange( 1,1000 - a - b):

p = a + b + c

如果p< 1000:

如果** 2 + b ** 2 == c ** 2:

解决方案[p] + = 1


max = 0

maxIndex = 0

index = 0

解决方案解决方案:

如果解决方案最大:

max =解决方案

maxIndex = index

指数+ = 1


打印maxIndex


2.4GHz Core2Duo MacBook需要2分12秒

Pro。感到惊讶的是我实施了相同的算法在

C:


#include< stdio.h>

#include< stdlib.h>


int main(){

int * solutions = calloc(1000,sizeof(int));


int p;

for(int a = 1; a< 1000; ++ a){

for(int b = 1; b <1000 - a; ++ b){

for(int c = 1; c <1000 - a - b; ++ c){

p = a + b + c;

if(p <1000){

if(a * a + b * b == c * c){

解决方案[p] + = 1;

}

}

}

}

}


int max = 0;

int maxIndex = 0;


for(int i = 0; i< 1000; ++ i){

if(solutions [i] max){

max = solutions [i];

maxIndex = i;

}

}

printf("%d \ n",maxIndex );

返回0;


}


gcc -o 39 -std = c99 -O3 39.c


生成的可执行文件需要0.24秒才能运行。我不希望使用脚本语言比本机代码运行得更快,但我感到惊讶于在这种情况下它的速度有多慢。关于什么

在上面的代码中导致python如此麻烦的任何想法?
I''m pretty new to python, but am very happy with it. As well as using
it at work I''ve been using it to solve various puzzles on the Project
Euler site -http://projecteuler.net. So far it has not let me down,
but it has proved surprisingly slow on one puzzle.

The puzzle is: p is the perimeter of a right angle triangle with
integral length sides, {a,b,c}. which value of p < 1000, is the
number of solutions {a,b,c} maximised?

Here''s my python code:

#!/usr/local/bin/python

solutions = [0] * 1001
p = 0

for a in xrange(1, 1000):
for b in xrange(1, 1000 - a):
for c in xrange(1, 1000 - a - b):
p = a + b + c
if p < 1000:
if a ** 2 + b ** 2 == c ** 2:
solutions[p] += 1

max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1

print maxIndex

It takes 2 minutes and twelve seconds on a 2.4GHz Core2Duo MacBook
Pro. Surprised at how slow it was I implemented the same algorithm in
C:

#include <stdio.h>
#include <stdlib.h>

int main() {
int* solutions = calloc(1000, sizeof(int));

int p;
for(int a = 1; a < 1000; ++a) {
for(int b = 1; b < 1000 - a; ++b) {
for(int c = 1; c < 1000 - a - b; ++c) {
p = a + b + c;
if(p < 1000) {
if(a * a + b * b == c * c) {
solutions[p] += 1;
}
}
}
}
}

int max = 0;
int maxIndex = 0;

for(int i = 0; i < 1000; ++i) {
if(solutions[i] max) {
max = solutions[i];
maxIndex = i;
}
}
printf("%d\n", maxIndex);
return 0;

}

gcc -o 39 -std=c99 -O3 39.c

The resulting executable takes 0.24 seconds to run. I''m not expecting
a scripting language to run faster than native code, but I was
surprised at how much slower it was in this case. Any ideas as to what
is causing python so much trouble in the above code?



来自数学导入sqrt


solutions = [0] * 1001

p = 0


for a xrange(1,1000):

a2 = a * a

for b in xrange(1,1000) - a):

c = sqrt(a2 + b * b)

如果c == int(c)且a + b + c< 1000:

解决方案[a + b + int(c)] + = 1


max = 0

maxIndex = 0

index = 0

解决方案解决方案:

如果解决方案最大值:

max =解决方案
maxIndex = index

index + = 1

print maxIndex


-

Arnaud

from math import sqrt

solutions = [0] * 1001
p = 0

for a in xrange(1, 1000):
a2 = a*a
for b in xrange(1, 1000 - a):
c = sqrt(a2 + b*b)
if c == int(c) and a+b+c < 1000:
solutions[a+b+int(c)] += 1

max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1

print maxIndex

--
Arnaud


9月2日上午7:20,Arnaud Delobelle< arno ... @ googlemail.comwrote:
On Sep 2, 7:20 am, Arnaud Delobelle <arno...@googlemail.comwrote:

9月2日下午12:51,jwrweather ... @ gmail.com写道:
On Sep 2, 12:51 pm, jwrweather...@gmail.com wrote:

我很漂亮python的新手,但我很高兴。除了使用

它在工作中我一直用它来解决项目中的各种难题

Euler网站-http://projecteuler.net。到目前为止,它并没有让我失望,但是它已经证明在一个谜题上出乎意料地缓慢。
I''m pretty new to python, but am very happy with it. As well as using
it at work I''ve been using it to solve various puzzles on the Project
Euler site -http://projecteuler.net. So far it has not let me down,
but it has proved surprisingly slow on one puzzle.


拼图是:p是直角三角形的周长,

整数长边,{a,b ,C}。 p的值是多少? 1000,

解决方案的数量{a,b,c}最大化了吗?
The puzzle is: p is the perimeter of a right angle triangle with
integral length sides, {a,b,c}. which value of p < 1000, is the
number of solutions {a,b,c} maximised?


这里是我的python代码:
Here''s my python code:


#! / usr / local / bin / python
#!/usr/local/bin/python


solutions = [0] * 1001

p = 0
solutions = [0] * 1001
p = 0


for a xrange(1,1000):

for b in xrange(1,1000-a):
$ b $ x for c in xrange(1,1000-a-b):

p = a + b + c

如果p< 1000:

如果** 2 + b ** 2 == c ** 2:

解决方案[p] + = 1
for a in xrange(1, 1000):
for b in xrange(1, 1000 - a):
for c in xrange(1, 1000 - a - b):
p = a + b + c
if p < 1000:
if a ** 2 + b ** 2 == c ** 2:
solutions[p] += 1


max = 0

maxIndex = 0

index = 0

解决方案中的解决方案:

如果解决方案最大值:

max =解决方案

maxIndex = index

index + = 1
max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1


print maxIndex
print maxIndex


2.4GHz Core2Duo需要2分12秒MacBook

Pro。感到惊讶的是我在多慢的时候实现了相同的算法

C:
It takes 2 minutes and twelve seconds on a 2.4GHz Core2Duo MacBook
Pro. Surprised at how slow it was I implemented the same algorithm in
C:


#include< stdio.h>

#include< stdlib.h>
#include <stdio.h>
#include <stdlib.h>


int main(){

int * solutions = calloc(1000,sizeof(int));
int main() {
int* solutions = calloc(1000, sizeof(int));


int p;

for(int a = 1; a< 1000; ++ a){
b = 1; b <1000 - a; ++ b){$ / b $ b $(b = 1; c <1000 - a - b; + + c){

p = a + b + c;

if(p <1000){

if(a * a + b) * b == c * c){

解决方案[p] + = 1;

}

}

}

}

}
int p;
for(int a = 1; a < 1000; ++a) {
for(int b = 1; b < 1000 - a; ++b) {
for(int c = 1; c < 1000 - a - b; ++c) {
p = a + b + c;
if(p < 1000) {
if(a * a + b * b == c * c) {
solutions[p] += 1;
}
}
}
}
}


int max = 0;

int maxIndex = 0;
int max = 0;
int maxIndex = 0;


for(int i = 0; i< 1000; ++ i){

if(solutions [i ] max){

max = solutions [i];

maxIndex = i;

}

}

printf("%d \ n",maxIndex);

返回0;
for(int i = 0; i < 1000; ++i) {
if(solutions[i] max) {
max = solutions[i];
maxIndex = i;
}
}
printf("%d\n", maxIndex);
return 0;


}
}


gcc -o 39 -std = c99 - O3 39.c
gcc -o 39 -std=c99 -O3 39.c


生成的可执行文件需要0.24秒才能运行。我不希望使用脚本语言比本机代码运行得更快,但我感到惊讶于在这种情况下它的速度有多慢。关于什么

在上面的代码中导致python如此麻烦的任何想法?
The resulting executable takes 0.24 seconds to run. I''m not expecting
a scripting language to run faster than native code, but I was
surprised at how much slower it was in this case. Any ideas as to what
is causing python so much trouble in the above code?



来自数学导入sqrt


solutions = [0] * 1001

p = 0


for a xrange(1,1000):

a2 = a * a

for b in xrange(1,1000) - a):

c = sqrt(a2 + b * b)

如果c == int(c)且a + b + c< 1000:

解决方案[a + b + int(c)] + = 1


max = 0

maxIndex = 0

index = 0

解决方案解决方案:

如果解决方案最大值:

max =解决方案
maxIndex = index

index + = 1

print maxIndex


-

Arnaud


from math import sqrt

solutions = [0] * 1001
p = 0

for a in xrange(1, 1000):
a2 = a*a
for b in xrange(1, 1000 - a):
c = sqrt(a2 + b*b)
if c == int(c) and a+b+c < 1000:
solutions[a+b+int(c)] += 1

max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1

print maxIndex

--
Arnaud



好​​奇:


O O + P A A + P
======= ======= ======= =======

2:22.56 0:25.65 0:00.75 0:00.20


O =原始实施

P = Psyco(psyco.full())

A = Arnaud''修改后的实施

For the curious:

O O + P A A + P
======= ======= ======= =======
2:22.56 0:25.65 0:00.75 0:00.20

O = Original Implementation
P = Psyco (psyco.full())
A = Arnaud''s Revised Implementation


jw ** *********@gmail.com 写在

新闻:11 ******************** *@r34g2000hsd.googlegro ups.com:
jw***********@gmail.com wrote in
news:11*********************@r34g2000hsd.googlegro ups.com:

拼图是:p是直角三角形的周长,

积分长度边s,{a,b,c}。 p的值是多少? 1000,

解决方案的数量{a,b,c}最大化了吗?


这里是我的python代码:


#!/ usr / local / bin / python

解决方案= [0] * 1001

p = 0


表示in xrange(1,1000):

for b in xrange(1,1000-a):

表示c in xrange( 1,1000 - a - b):

p = a + b + c

如果p< 1000:

如果** 2 + b ** 2 == c ** 2:

解决方案[p] + = 1
The puzzle is: p is the perimeter of a right angle triangle with
integral length sides, {a,b,c}. which value of p < 1000, is the
number of solutions {a,b,c} maximised?

Here''s my python code:

#!/usr/local/bin/python

solutions = [0] * 1001
p = 0

for a in xrange(1, 1000):
for b in xrange(1, 1000 - a):
for c in xrange(1, 1000 - a - b):
p = a + b + c
if p < 1000:
if a ** 2 + b ** 2 == c ** 2:
solutions[p] += 1



一旦p> = 1000,它就不会再回来了。如果你在这之后突破了

最里面的循环,你将节省一大堆

的时间。

Once p >= 1000, it ain''t goin'' back. If you break out of the
innermost loop here after that happens, you''ll save a bunch of
time.


max = 0

maxIndex = 0

index = 0

解决方案解决方案:

如果解决方案最大:

max =解决方案

maxIndex = index

指数+ = 1


打印maxIndex


2.4GHz Core2Duo需要2分12秒

MacBook Pro。
max = 0
maxIndex = 0
index = 0
for solution in solutions:
if solution max:
max = solution
maxIndex = index
index += 1

print maxIndex
It takes 2 minutes and twelve seconds on a 2.4GHz Core2Duo
MacBook Pro.



[...]

[...]


生成的可执行文件需要0.24秒才能运行。我不是想要一种脚本语言比本机代码运行得更快,而不是
但我对这种情况下的速度有多慢感到惊讶。在上面的

代码中有什么导致python这么麻烦的想法?

想法?
The resulting executable takes 0.24 seconds to run. I''m not
expecting a scripting language to run faster than native code,
but I was surprised at how much slower it was in this case. Any
ideas as to what is causing python so much trouble in the above
code?


这篇关于为什么这个循环繁重的代码在Python中如此之慢?可能的项目Euler剧透的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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