他の言語と比較した Python のパフォーマンスに関する一般的な質問が多数あります。より具体的な例があります: python と c# で記述された 2 つの単純な関数があり、両方とも int 番号が素数であるかどうかをチェックします。
パイソン:
import time
def is_prime(n):
num =n/2
while num >1:
if n % num ==0:
return 0
num-=1
return 1
start = time.clock()
probably_prime = is_prime(2147483629)
elapsed = (time.clock() - start)
print 'time : '+str(elapsed)
およびC#:
using System.Diagnostics;
public static bool IsPrime(int n)
{
int num = n/2;
while(num >1)
{
if(n%num ==0)
{
return false;
}
num-=1;
}
return true;
}
Stopwatch sw = new Stopwatch();
sw.Start();
bool result = Functions.IsPrime(2147483629);
sw.Stop();
Console.WriteLine("time: {0}", sw.Elapsed);
そして時間(Pythonの初心者としての私にとっては驚きです:)):
パイソン: 121秒; c#: 6 秒
この大きな違いはどこから来るのか説明していただけますか?