Did you code this up in Python too? There's a built-in Julia function
called primes (written in pure
Julia<https://github.com/JuliaLang/julia/blob/master/base/primes.jl>),
which implements a prime number sieve efficiently:

julia> @time primes(10000000);
elapsed time: 0.167461201 seconds (6581064 bytes allocated)

Obviously the times are apples to oranges since we're on different
machines, but it looks to be comparable to your C version. Also, on my
system, your version runs in much less time:

julia> @time primes2(10000000);
elapsed time: 0.185365732 seconds (80021608 bytes allocated)

What version of Julia are you running and how are you timing it? Are you
perchance timing the execution of the program from the shell?


On Thu, Feb 13, 2014 at 10:26 AM, James King <[email protected]> wrote:

> When I'm learning a new language one of the programs I write is a simple
> sieve of Eratosthenes; the julia code is shown below.
> Julia is taking 70% longer than python 2.7.5 to execute the program - have
> I coded something incorrectly?
>
>  Language Execution time (seconds)  C 0.106   pypy 0.323   python 1.26
> julia 2.177
>
>
> function primes(N)
>     sieveTo = ceil(sqrt(N))
>     S = ones(N)
>     S[1] = 0
>     for i in 1:sieveTo
>         if S[i] > 0
>             for j in i*i:i:N
>                 S[j] = 0
>             end
>         end
>     end
>     return(sum(S))
> end
>
> println(primes(10000000))
>

Reply via email to