2005年11月6日星期日

calculation speed comparison of C,Java,Python

platform) windows xp
version)
C: gcc version 3.3.1 (mingw special 20030804-1)
Java: JDK1.5.0
Python: Python 2.4.2
program)
C:

#include
#include
#include
void printPrime(int start, int count);
int isPrime(int x);
int main(){
DWORD t1,t2;
t1=timeGetTime();
printPrime(10,2000);
t2=timeGetTime();
printf("%d\
", (t2-t1));
}

void printPrime(int start, int count){
while(1){
if (isPrime(start)){
count--;
printf("%i\
",start);
}
start++;
if (count<=0){
return;
}
}
}
int isPrime(int x){
int i;
for (i=2; i if (x%i==0){
return 0;
}
}
return 1;
}

Java:

public class Test {
public static void main(String[] args){
run();
}
static void run(){
long t1=System.currentTimeMillis();
printPrime(10,2000);
System.out.println((System.currentTimeMillis()-t1)+" ms");
}

static void printPrime(int start, int count){
while(true){
if (isPrime(start)){
count--;
System.out.println(start);
}
start++;
if (count<=0){
return;
}
}
}
static boolean isPrime(int x){
for (int i=2; i if (x%i==0){
return false;
}
}
return true;
}
}

Python:

def printPrime(start, count):
while True:
if isPrime(start):
count-=1
print start
start+=1
if count <= 0:
return
def isPrime(x):
for i in range(2,x/2):
if x % i==0:
return False
return True

from time import time
t1=time()
printPrime(10, 2000)
print time()-t1

excution)
redirect stdout to a file

Result)
C: 230 ms
Java: 460 ms
Python: 7800 ms

Date) 2005/11/05


-----

没有评论:

博客归档

neoedmund's shared items

我的简介

ZIP Code File