# Julia!

I am quite excited about the Julia language (windows download, manual). It’s free. It’s almost the same as Matlab, but it is as fast as C++ (much faster than Matlab and Octave, 160 times faster in the example below). Here is a quick comparison.

Matlab code (primeQ.m):

function b = primeQ( i )
for j=2:ceil(i/2.0)
if mod(i,j) == 0
b = false;
return
end
end
b = true;
end


Matlab input:

tic; primeQ(71378569); toc

Matlab output:

Elapsed time is 52.608765 seconds.

Julia code (primeQ.jl):

function primeQ( i )
for j=2:ceil(i/2.0)
if mod(i,j) == 0
return false;
end
end
return true
end


Julia input:

tic(); primeQ(71378569); toc()

Julia output:

elapsed time: 0.3280000686645508 seconds

1. My friend Chris wrote:

Interesting. A quick check with a C++ program to do the same thing suggests that Julia is giving performance comparable to C++ — assuming Hein and I were using the same computer, which we’re not.

Here's my code:
#include <cmath>
#include <iostream>
#include <sys/time.h>
using namespace std;

bool primeQ(long i){
for (int j = 2; j < ceil((double)i/2.0); j++){
if (i % j == 0){
return false;
}
}
return true;
}

int main(int argc, char** argv){
timeval time;
gettimeofday(&time, NULL);
long millis1 = (time.tv_sec * 1000) + (time.tv_usec / 1000);
bool b = primeQ(71378569);

gettimeofday(&time, NULL);
long millis2 = (time.tv_sec * 1000) + (time.tv_usec / 1000);
if (b){
cout << "Prime!" << endl;
}else{
cout << "Not so prime!" << endl;
}
cout << (double)(millis2 - millis1) / (double)1000 << " seconds" << endl;
}

The running time is about ~0.32 seconds when I run assuming I compile with –O3 for optimization.

Cheers,
Chris

2. Glen wrote:

Be careful what conclusions you make!

We went through this with Fortran compilers 30 years ago.
A machine from Gould was so much faster than the VAX

Turns out that the Gould compiler did some dependancy
analysis and pruned all code where the results were
never used. So we added the result of the loop to an
accumulator. And that one REAL value was printed at the
end. The run time for the Gould went up by 3 orders of
magnitude and was then close to the VAX.

If Julia’s numbers hold up with different code, I am happy.
I’m just saying don’t let yourself get faked out by a smart
compiler.

– G

3. It may be fast but if I have to rewrite all the Matlab toolboxes it’s slow.

4. My friend Glen suggested the benchmarks below. The results were 25 seconds for the C program and 11.06 seconds for Julia.

Julia code:

function bench11( iMax )
a=1000.00;
f = 11.0;
for i=0:(iMax-1)
b=float32(i)
c=sin(b/a)
d=cos(b/a)
f=sqrt(c*c + d*d) + c + d + f
end
f
end

Julia interactive session:

julia> tic(); bench11(100000000); toc()
elapsed time: 11.062000036239624 seconds
11.062000036239624

julia> bench11(100000000)
1.0000204708268365e8

C++ code:

#include <stdio.h>
#include <math.h>

int main()
{
double a,b,c,d,e;
//double sin(), cos(), sqrt();
register int i;

a=1000.00;
e= 11.0;
for (i=0; i < 100000000; i++)
{
b=(double) i;
c=sin(b/a);
d=cos(b/a);
e=sqrt(c*c + d*d) + c + d + e;
}

printf("e = %10.5f\n", (float) e);

return 1;

Call to the C code:

date; ./a.exe; date
Wed Aug 29 10:50:35 EDT 2012
e = 100002048.00000
Wed Aug 29 10:51:06 EDT 2012