Thanks for the quick reply. Yes these were all hand coded in Julia, python,
and C - same algorithm.
Version 0.1.2
02f3159-Linux-amd64 (2013-05-15 23:25:17)
I was timing from the shell, my function is much faster timed from within
julia:
julia> @time myprimes(10000000)
elapsed time: 0.23879289627075195 seconds
(about 2 x C time)
I don't seem to have built in julia function for primes?
julia> primes(10000000)
ERROR: primes not defined
perhaps I am missing some package?
C code below, in case you want to compare:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
uint64_t sieve (uint64_t N)
{
uint64_t sieve_to = ceil(sqrt (N));
uint64_t pi = 0;
uint8_t* S = malloc (sizeof (uint8_t) * (N + 1));
uint64_t i, j;
memset (S, 1, sizeof (uint8_t) * N);
S[0] = 0; S[1] = 0;
for (i = 1; i <= sieve_to; i++)
if (S[i] == 1)
for (j = i*i; j <= N; j += i)
S[j] = 0;
for (i = 1; i<= N; i++) pi += S[i];
free (S);
return (pi);
}
int main()
{
printf ("%lu\n", sieve(10000000));
return(0);
}
On Thursday, February 13, 2014 10:34:32 AM UTC-5, Stefan Karpinski wrote:
>
> 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]<javascript:>
> > 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))
>>
>
>