Dear Julia users,

I am considering using Julia for computational projects. 
As a first to get a feeling of the new language a I tried to benchmark 
Julia speed against other popular languages.
I used an example code from the Cython tutorial: 
http://docs.cython.org/src/tutorial/cython_tutorial.html [ the code for 
finding n first prime numbers]. 

Rewriting the code in different languages and measuring the times on my 
Windows laptop gave me the following results:

Language | Time in seconds (less=better)

Python: 65.5
Cython (with MinGW): 0.82
Java : 0.64
Java (with -server option) : 0.64
C (with MinGW): 0.64
Julia (0.2): 2.1
Julia (0.3 nightly build): 2.1

All the codes for my experiments are attached to this post (Cython i Python 
are both being run starting from the prim.py file)

The thing that worries me is that Julia takes much much longer than Cython 
,,,
I am a beginner to Julia and would like to kindly ask what am I doing wrong 
with my code. 
I start Julia console and use the command  include ("prime.jl") to execute 
it.

This code looks very simple and I think the compiler should be able to 
optimise it to at least the speed of Cython?
Maybe I my code has been written in non-Julia style way and the compiler 
has problems with it?

I will be grateful for any answers or comments.

Best regards,
Przemyslaw Szufel

Attachment: prime.jl
Description: Binary data

Attachment: primes.pyx
Description: Binary data


import pyximport; pyximport.install(setup_args={'options': {'build_ext': {'compiler': 'mingw32'}}})


import primes
from timeit import timeit


def test1():
    print(sum(primes.primes_list(20000)))
    
    
def test2():
    p = [0]*20000
    result = []    
    k = 0
    n = 2
    while k < 20000:
        i = 0
        while i < k and n % p[i] != 0:
            i = i + 1
        if i == k:
            p[k] = n
            k = k + 1
            result.append(n)
        n = n + 1
    print(sum(result))


print("Cython:",timeit("test1()", setup="from __main__ import test1",number=1))
print("Python:",timeit("test2()", setup="from __main__ import test2",number=1))

print("Cython:",timeit("test1()", setup="from __main__ import test1",number=1))
print("Python:",timeit("test2()", setup="from __main__ import test2",number=1))


import java.util.ArrayList;

public class Primes {

	static ArrayList<Integer> primes_list (int kmax) {
	    int n, k, i;
	    int p[] = new int[20000];

	    ArrayList<Integer> result = new ArrayList<>();
	    
	    k = 0;
	    n = 2;
	    while (k < kmax) {
	        i = 0;
	        while (i < k && n % p[i] != 0) {
	            i = i + 1;
	        }
	        if (i == k) {
	            p[k] = n;
	            k = k + 1;
	            result.add(n);
	        }
	        n = n + 1;
	        
	    }
	    return result;
	}
	public static void main(String[] args) {
		long start;
		int sum;
		start = System.currentTimeMillis();
		sum=0;
		for (int s : primes_list(20000)) sum+=s;
		System.out.println(sum);
		System.out.println((System.currentTimeMillis()-start)/1000.0);
		
		start = System.currentTimeMillis();
		sum=0;
		for (int s : primes_list(20000)) sum+=s;
		System.out.println(sum);
		System.out.println((System.currentTimeMillis()-start)/1000.0);
	}

}
#include <stdio.h>
#include <time.h>

int main() {
    int n, k, i;
    int p[20000];
    int result;
    clock_t t1, t2;

    t1 = clock();
    k = 0;
    n = 2;
    result = 0;
    while (k < 20000) {
        i = 0;
        while (i < k && n % p[i] != 0) {
            i++;
        }
        if (i == k) {
            p[k] = n;
            k++;
            result += n;
        }
        n++;
    }
    printf("result: %d\n", result);
    t2 = clock();
    float diff = (((float)t2 - (float)t1) / 1000000.0F ) * 1000;
    printf("Time: %f\n",diff);
    return 0;
}

Reply via email to